magic-methods

Best Practices for __get() and __set() [closed]

这一生的挚爱 提交于 2020-03-02 05:18:26
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Stemming from this question on using __get() and __set() to access private variables, I'd like to get the input on how they are used

Creating Yii FormModel objects (CFormModel) dynamically

自作多情 提交于 2020-01-22 08:05:27
问题 I'm working on an application that involves generating forms at a high level of abstraction (it's a CMS app). I want to dynamically create CFormModel objects and set the form fields on-the-fly. I think I can do this by extending CFormModel, and then dynamically creating the class properites that represent the form fields ('attributes' in the Yii lingo). To illustrate, instead of specifying a login form in the following class (defined in a file): // From: http://www.yiiframework.com/doc/guide

Creating Yii FormModel objects (CFormModel) dynamically

社会主义新天地 提交于 2020-01-22 08:05:02
问题 I'm working on an application that involves generating forms at a high level of abstraction (it's a CMS app). I want to dynamically create CFormModel objects and set the form fields on-the-fly. I think I can do this by extending CFormModel, and then dynamically creating the class properites that represent the form fields ('attributes' in the Yii lingo). To illustrate, instead of specifying a login form in the following class (defined in a file): // From: http://www.yiiframework.com/doc/guide

Creating Yii FormModel objects (CFormModel) dynamically

非 Y 不嫁゛ 提交于 2020-01-22 08:04:21
问题 I'm working on an application that involves generating forms at a high level of abstraction (it's a CMS app). I want to dynamically create CFormModel objects and set the form fields on-the-fly. I think I can do this by extending CFormModel, and then dynamically creating the class properites that represent the form fields ('attributes' in the Yii lingo). To illustrate, instead of specifying a login form in the following class (defined in a file): // From: http://www.yiiframework.com/doc/guide

To make objects of a custom class comparable, is it enough to define just a few of the members in `__eq__` and `__lt__` family?

北慕城南 提交于 2020-01-15 05:25:07
问题 Let's say I have a class, members of which I want to compare with the usual operators == , < , <= , > , and >= . As I understand, this could be accomplished by initializing defining magic method __cmp__(a, b) which returns -1 ( a < b ), 0 ( a == b ), or 1 ( a > b ). It seems like __cmp__ was deprecated since Python 3 in favor of defining __eq__ , __lt__ , __le__ , __gt__ , and _ge__ methods separately. I defined __eq__ and __lt__ assuming that the default values for __le__ would look

Possible to test if a variable is static in PHP?

假装没事ソ 提交于 2020-01-12 23:04:32
问题 Is it possible to test if a variable is static in PHP? I am trying create a magic method __get that also looks at static variables. I find that property_exists() returns true when a variable is static too. But I will need to use :: instead of -> I'd expect? 回答1: It is possible to test if a variable is static via Reflection: class Foo { static $bar; } $prop = new ReflectionProperty('Foo', 'bar'); var_dump($prop->isStatic()); // TRUE However, that still won't allow you to use them with magic

Are Magic Methods Best practice in PHP? [closed]

风流意气都作罢 提交于 2020-01-09 13:09:46
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Are Magic Methods Best practice in PHP? 回答1: I don't think magic methods are best or worst practice: depending on what you want to

Are Magic Methods Best practice in PHP? [closed]

丶灬走出姿态 提交于 2020-01-09 13:09:31
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Are Magic Methods Best practice in PHP? 回答1: I don't think magic methods are best or worst practice: depending on what you want to

How to access multiple properties with magic method(__get & __set)?

橙三吉。 提交于 2020-01-04 13:23:46
问题 I recently studied magic methods, __get and __set , and was wondering how to actually set and get multiple properties in the class. I know it works perfectly with only one variable or array, but I'm not sure about accessing multiple variables. Is there anyone who could explain this to me? class myMagic2 { public $data; public $name; public $age; public function __set($item, $value) { $this->item = $value; } public function __get($item){ return $this->item; } } Is there a way to access all

php magic method to catch array_push

我只是一个虾纸丫 提交于 2020-01-02 08:55:32
问题 I am looking for a way to intercept the action in array_push, because when it will be retrieve it each value of the array has another info like: class ClassName { var $test = array(); function __set($attr, $value) { $this->$attr = 'My extra value'.$value; } function index(){ array_push($this->test, "some val"); array_push($this->test, "some other val"); print_r($this->test); } } $o = new ClassName(); $o->index(); And expected to get something like: Array ( [0] => My extra value some val [1] =