PHP版本ASP.NET/C#财产属性类的 [英] PHP version of ASP.NET/C# property attribute class

查看:166
本文介绍了PHP版本ASP.NET/C#财产属性类的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有这样的事情吗?

Is there such a thing?

我想要做这样的事情在PHP,但我看不到如何从PHP文档做到这一点:

I'd like to do something like this in PHP but I can't see how to do it from the PHP docs:

public class User : ValidationBase
{  
  [NotNullOrEmpty(Message = "Please enter a user name.")] 
  public string UserName { get; set; } 
}

我要找的是PHP等价物ASP.NET/C#财产属性,在上面的例子由 [NotNullOrEmpty(消息标志着=请输入一个用户名。 )] 行财产申报以上。

推荐答案

PHP已经宣告属性没有内置的机制,但它是可以使用一些自定义的code来模拟这种行为。其基本思想是把一个注释块元数据,然后写了解析注释块的类。这比C#不太方便,因为你需要确保你对此有何评论属性的格式正确,但它的作品。

PHP has no built-in mechanism for declaring attributes, but it is possible to simulate this behavior using some custom code. The basic idea is to place your metadata in a comment block, and then write a class that parses that comment block. This is less convenient than C# because you need to ensure that your comment "attributes" are formatted properly, but it works.

下面是如何做到这一点的例子:的http://web.archive.org/web/20130302084638/http://interfacelab.com/metadataattributes-in-php/

Here is an example of how to do this: http://web.archive.org/web/20130302084638/http://interfacelab.com/metadataattributes-in-php/

这篇关于PHP版本ASP.NET/C#财产属性类的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆