magic-methods

__get is not called if __set is not called, however code works?

守給你的承諾、 提交于 2020-01-02 04:01:26
问题 Here is my code: <?php class SampleClass { public function __get($name){ echo "get called"; echo $name; } public function __set($name, $value) { echo "set called"; } } ?> And my index file: $object = new SampleClass(); $object->color = "black"; echo $object->color; If I run this code as it is, here is the output: set calledget calledcolor However if I comment out public function __set($name, $value) { echo "set called"; } the part above (only this part), then the output will be: black So what

Is there a way to return a custom value for min and max in Python?

痞子三分冷 提交于 2020-01-01 01:33:15
问题 I have a custom class, class A: def __init__(self, a, b): self.a = a self.b = b The class is not iterable or indexable or anything like that. If at all possible, I would like to keep it that way. Is it possible to have something like the following work? >>> x = A(1, 2) >>> min(x) 1 >>> max(x) 2 What got me thinking about this is that min and max are listed as "Common Sequence Operations" in the docs. Since range is considered to be a sequence type by the very same docs, I was thinking that

Where is the Python documentation for the special methods? (__init__, __new__, __len__, …)

删除回忆录丶 提交于 2019-12-28 11:38:12
问题 Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__ , __new__ , __len__ , __add__ ) 回答1: Please take a look at the special method names section in the Python language reference. 回答2: Dive Into Python has an excellent appendix for them. 回答3: If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation link from the accepted answer. __abs__ __add__ __and__ __call__ __class__ __cmp__ _

Where is the Python documentation for the special methods? (__init__, __new__, __len__, …)

我怕爱的太早我们不能终老 提交于 2019-12-28 11:35:03
问题 Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__ , __new__ , __len__ , __add__ ) 回答1: Please take a look at the special method names section in the Python language reference. 回答2: Dive Into Python has an excellent appendix for them. 回答3: If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation link from the accepted answer. __abs__ __add__ __and__ __call__ __class__ __cmp__ _

Where is the Python documentation for the special methods? (__init__, __new__, __len__, …)

蹲街弑〆低调 提交于 2019-12-28 11:33:00
问题 Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__ , __new__ , __len__ , __add__ ) 回答1: Please take a look at the special method names section in the Python language reference. 回答2: Dive Into Python has an excellent appendix for them. 回答3: If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation link from the accepted answer. __abs__ __add__ __and__ __call__ __class__ __cmp__ _

Any method to denote object assignment?

回眸只為那壹抹淺笑 提交于 2019-12-25 05:20:28
问题 I've been studying magic methods in Python, and have been wondering if there's a way to outline the specific action of: a = MyClass(*params).method() versus: MyClass(*params).method() In the sense that, perhaps, I may want to return a list that has been split on the '\n' character, versus dumping the raw list into the variable a that keeps the '\n' intact. Is there a way to ask Python if its next action is about to return a value to a variable, and change action, if that's the case? I was

ArrayAccess/ArrayObject do not work with functions like call_user_func_array() [closed]

跟風遠走 提交于 2019-12-24 10:23:17
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . When implementing an object using ArrayAccess or ArrayObject , to some operations it's a perfectly normal array (for instance a foreach() statement).

Using magic constants from another scope

笑着哭i 提交于 2019-12-24 09:35:36
问题 I created a class Debug in which all properties and methods are static. Using late static binding I use this class as a logger of what is being done and at which moment (in fact I'm testing now performance issue, so I would like to now what and when goes). So at the moment I have something in each main method of each class like Debug::log(__CLASS__ . '::' . __METHOD__); . In Debug::log() method I can add time and store it in some array. If I'd wanted some day to change behaviour I would need

Working with __get() by reference

女生的网名这么多〃 提交于 2019-12-23 14:23:04
问题 With an example class such as this: class Test{ public function &__get($name){ print_r($name); } } An instance of Test will kick back output as such: $myTest = new Test; $myTest->foo['bar']['hello'] = 'world'; //outputs only foo Is there a way I can get more information about what dimension of the array is being accessed, showing me (from the previous example) that the bar element of foo , and the hello element of bar are being targeted? 回答1: You can't with the current implementation. In

Magic getters and setters in Enterprise Architect

佐手、 提交于 2019-12-23 12:13:29
问题 I'm using Enterprise Architect to make a UML class diagram and generate PHP5 code with it. Using this, one can make getters and setters for an attribute, which looks like this in the code (only relevant lines shown): private $id; public function getId() { return $this->id; } /** * * @param newVal */ public function setId($newVal) { $this->id = $newVal; } I'd like to use the magic methods __get($property) and __set($property, $value) instead of seperate methods for each property. Is it