Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to realize my own MVC framework and invented a very nice way to provide definitions of virtual fields and additional relations.

According to some other high-voted post on stackoverflow, this should actually work:

class User extends Model {

  public $hasOne = array('UserSetting');

  public $validate = array();

  public $virtualFields = array(
      'fullname' => function () {
          return $this->fname . ($this->mname ? ' ' . $this->mname : '') . ' ' . $this->lname;
      },
      'official_fullname' => function () {

      }
  );
}

But it doesn't work. It says: Parse error: syntax error, unexpected T_FUNCTION. What am I doing wrong?

PS. talking about this one Can you store a function in a PHP array?

share|improve this question

closed as off-topic by cryptic ツ, Benjamin Gruenbaum, rdlowrey, hakre, PeeHaa Jun 26 '13 at 18:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – cryptic ツ, Benjamin Gruenbaum, rdlowrey, hakre
If this question can be reworded to fit the rules in the help center, please edit the question.

    
no, I've checked it. It is 5.3.8. –  Barth Zalewski Jun 25 '13 at 17:21
2  
Attribute declarations in a class definition can only be constant values, not expressions.. And anonymous functions simply aren't primitive types or structures. –  mario Jun 25 '13 at 17:23

1 Answer 1

up vote 3 down vote accepted

You must define the methods in the constructor, or some other method, not directly in the class member declaration.

class User extends Model {

  public $hasOne = array('UserSetting');

  public $validate = array();

  public $virtualFields = array();

  public function __construct() {
     $this->virtualFields = array(
        'fullname' => function () {
            return $this->fname . ($this->mname ? ' ' . $this->mname : '') . ' ' . $this->lname;
        },
        'official_fullname' => function () {

        }
    );
  }
}

While that works, PHP's magic method __get() is better suited to this task:

class User extends Model {

  public $hasOne = array('UserSetting');

  public $validate = array();

  public function __get($key) {
     switch ($key) {
       case 'fullname':
           return $this->fname . ($this->mname ? ' ' . $this->mname : '') . ' ' . $this->lname;
       break;

       case 'official_fullname':
         return '';
       break;
    };
  }
}
share|improve this answer
    
It worked. Thank you. It's a little pity, however, because it isn't that clear and elegant as my idea originally was. Also because my constructor is a little bit more complicated and can accept some more arguments, which have to be passed through parent::__construct. But well, it is still pretty cool. Thanks again. I +1-ed it. –  Barth Zalewski Jun 25 '13 at 17:28
1  
@BarthZalewski you may consider looking into PHP's magic methods, __get(), __call() and __callStatic(). Those methods are better suited to this sort of thing and will keep your constructor clean. php.net/manual/en/language.oop5.magic.php –  popthestack Jun 25 '13 at 17:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.