PHP version of the ASP.NET/C# property attribute class

Is there such a thing?

I would like to do something similar in PHP, but cannot figure out how to do this from PHP docs:

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

I am looking for the PHP equivalent of the ASP.NET/C# property attribute, which in the example above is indicated by a line [NotNullOrEmpty(Message = "Please enter a user name.")]above the property declaration.

+5
source share
8 answers

, , , PHP.

, , . Zend Framework Zend_Form, , , , , .

+1

PHP , .

, :

public function setUserName($str) {
    if (empty($str)) {
        throw new Exception('Please enter a user name.');
    }
    $this->userName = $str;
}

public function getUserName($str) {
    return $this->userName;
}
0

, ​​, PHP .

0

php .

- php, getter setter setter.

class User extends ValidationBase
{
    private $userName;
    public function GetUserName()
    {
        return $this->userName;
    }
    public function SetUserName($val = '')
    {
        if ($val === '')
        {
            return false;
        }
        else
        {
            $this->userName = $val;
            return true;
        }
    }
}
0

, PHP 5.0 , , PHP 5.0, # DocComment

. JSON DocComment , DocComment .

<?php
/** {"Description":"This is a test class"} */
class User {

   /** {"Message":"Please enter a user name."} */
   public $Username;

   /** {"Message":"Please enter a user name."} */
   public function Login($username) {
        print "Inside 'aMemberFunc()'";
    }
}

$rc = new ReflectionClass("User");  // class name
print $rc->getDocComment()                          . "<br />\n"; // Get Class comment
print $rc->getMethod("Login")->getDocComment()      . "<br />\n"; // Get Method comment
print $rc->getProperty("Username")->getDocComment() . "<br />\n"; // Get Property comment

?>

/** {"Description":"This is a test class"} */
/** {"Message":"Please enter a user name."} */
/** {"Message":"Please enter a user name."} */

, DocComment JSON, . reflection , , PHP

. Zend Zend , , , .

0

. ... ReflectionClass :: getDocComment

/**
* You can use doc comments in reflection. 
* See more info https://php.net/manual/en/reflectionclass.getdoccomment.php
* 
* @role("admin")
*/
class permissiontest extends Page
{
    public function render($p)
    {
        echo 'Howdy friend!';
        //$this->renderView($p);
    }
}

class handler 
{
    public function OnPageRendering($page)
    {
        $r = new ReflectionClass(get_class($page));
        $s = $r->getDocComment();
        if (!str::isNullOrEmpty(trim($s))) {
            // tags::pre($s);
            /**
             * @role("filter")
             */
            $matches = array();
            //@role("filter") https://regex101.com/
            preg_match('/@role\(\"(.*)\"\)/', $s, $matches);
            if (!arrays::isNullOrEmpty($matches)) {
            // tags::pre($matches);//Array ( [0] => @role("filter") [1] => filter ) 
            // tags::pre($matches[0]);//@role("filter")        
            // tags::pre($matches[1]);//filter
                $filter = $matches[1];
                //tags::h("Check Permission for:".$filter);
                if (!userHasRights($filter)) {
                    die('You do not have permission on this page! Process stopped!');
                }
            }

        }


    }
}
function userHasRights($right) {
    return false;
}
0

Source: https://habr.com/ru/post/1726352/


All Articles