Is there such a thing?
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; }
}
What I’m looking for is the PHP equivalent of an ASP.NET/C# property attribute, which in the above example is signified by the [NotNullOrEmpty(Message = "Please enter a user name.")] line above the property declaration.
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.
Here is an example of how to do this: http://web.archive.org/web/20130302084638/http://interfacelab.com/metadataattributes-in-php/