magic-methods

__callStatic(), call_user_func_array(), references, and PHP 5.3.1

[亡魂溺海] 提交于 2019-11-30 03:46:46
问题 I've been reading around on SO, and elsewhere, however I can't seem to find anything conclusive. Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below? While the example doesn't try to solve it, it certainly illustrates the problem: class TestClass{ // surely __call would result similarly public static function __callStatic($function, $arguments){ return call_user_func_array($function, $arguments); }

Use of PHP Magic Methods __sleep and __wakeup

空扰寡人 提交于 2019-11-29 23:41:35
What is the use of the __sleep and __wakeup magic methods in PHP? I read the PHP documentation but it's still not clear: class sleepWakeup { public function __construct() { // constructor // } public function __sleep() { echo 'Time to sleep.'; } public function __wakeup() { echo 'Time to wakeup.'; } } $ob = new sleepWakeup(); // call __sleep method echo $ob->__sleep(); echo "\n"; // call __wakeup method echo $ob->__wakeup(); This sample code prints: Time to sleep. Time to wakeup. If I were to rename __sleep and __wakeup to foo and bar then it does the same thing. What is the proper use of

Python __index__ special method

可紊 提交于 2019-11-29 17:14:25
问题 >>> class Thing(object): ... def __index__(self): ... return 1 ... >>> thing = Thing() >>> list_ = ['abc', 'def', 'ghi'] >>> list_[thing] 'def' >>> dict_ = {1: 'potato'} >>> dict_[thing] # KeyError How does thing know to represent itself as 1 when accessed by a list, but not by a dict? Don't both magic methods go through __getitem__ ? The usage shown for lists could go through __int__ instead so what is the raison d'être for __index__ anyway? 回答1: Dict and List does not implement __getitem__

Why PHP uses static methods in object context?

放肆的年华 提交于 2019-11-29 17:14:01
问题 I have the following code (like, for real, this is my real code) : <?php class Foobar { public static function foo() { exit('foo'); } } When I run $foobar = new FooBar; $foobar->foo() it displays foo . Why would PHP try to use a static method in an object context ? Is there a way to avoid this ? Ok you guys didn't get my problem : I know the differences between static and non static methods and how to call them. That's my whole point, if I call $foobar->foo() , why does PHP tries to run a

How to implement __iadd__ for a Python property

霸气de小男生 提交于 2019-11-29 09:22:27
I'm trying to create a Python property where in-place adding is handled by a different method than retrieving the value, adding another value and reassigning. So, for a property x on an object o , o.x += 5 should work differently than o.x = o.x + 5 The value of o.x should be the same in the end, so as not to confuse people's expectations, but I want to make the in-place add more efficient. (In reality the operation takes a lot more time than simple addition.) My first idea was to define, in the class, x = property(etc. etc.) x.__iadd__ = my_iadd But this raises an AttributeError, presumably

Triggering __call() in PHP even when method exists

[亡魂溺海] 提交于 2019-11-29 06:18:29
问题 The PHP documentation says the following about the __call() magic method: __call() is triggered when invoking inaccessible methods in an object context. Is there a way I can have __call() called even when a method exists, before the actual method is called? Or, is there some other hook I can implement or another way that would provide this functionality? If it matters, this is for a static function (and I would actually prefer to use __callStatic ). 回答1: How about just make all your other

Using PDO::FETCH_CLASS with Magic Methods

和自甴很熟 提交于 2019-11-29 04:06:48
I have a class that uses magic methods to store properties. Here is a simplified example: class Foo { protected $props; public function __construct(array $props = array()) { $this->props = $props; } public function __get($prop) { return $this->props[$prop]; } public function __set($prop, $val) { $this->props[$prop] = $val; } } I'm trying to instantiate objects of this class for each database row of a PDOStatement after it's executed, like this (doesn't work): $st->setFetchMode(PDO::FETCH_CLASS, 'Foo'); foreach ($st as $row) { var_dump($row); } The problem is that PDO::FETCH_CLASS does not seem

Why are explicit calls to magic methods slower than “sugared” syntax?

大憨熊 提交于 2019-11-29 02:52:34
问题 I was messing around with a small custom data object that needs to be hashable, comparable, and fast, when I ran into an odd-looking set of timing results. Some of the comparisons (and the hashing method) for this object simply delegate to an attribute, so I was using something like: def __hash__(self): return self.foo.__hash__() However upon testing, I discovered that hash(self.foo) is noticeably faster. Curious, I tested __eq__ , __ne__ , and the other magic comparisons, only to discover

Why is __getattribute__ not invoked on an implicit __getitem__-invocation?

浪尽此生 提交于 2019-11-29 02:08:10
While trying to wrap arbitrary objects, I came across a problem with dictionaries and lists. Investigating, I managed to come up with a simple piece of code whose behaviour I simply do not understand. I hope some of you can tell me what is going on: >>> class Cl(object): # simple class that prints (and suppresses) each attribute lookup ... def __getattribute__(self, name): ... print 'Access:', name ... >>> i = Cl() # instance of class >>> i.test # test that __getattribute__ override works Access: test >>> i.__getitem__ # test that it works for special functions, too Access: __getitem__ >>> i[

Are Magic Methods Best practice in PHP? [closed]

血红的双手。 提交于 2019-11-28 19:09:23
Are Magic Methods Best practice in PHP? I don't think magic methods are best or worst practice: depending on what you want to achieve you can use them or not... What I mean is that you don't have to tweak your code as possible to use them, but if you have to there is no problem at all. If you have an object with 3 and only 3 attributes you don't need to use magic setters/getters, but in some advanced cases they are a great way to do very complex things (ORM systems etc...) Maybe some of them are deprecated, I don't know, but most of them are not. cons Text searches don't find the functions