magic-methods

Is it possible, using PHPUnit mock objects, to expect a call to a magic __call() method?

旧巷老猫 提交于 2019-12-02 23:35:34
I've got a mock object in a test. The real object, PageRepository, implements a magic method using __call(), so if you call $pageRepository->findOneByXXXX($value_of_field_XXXX), it will search the database for records matching that parameter. Is there a way to mock that method? The real method call would look something like this: $homepage = $pageRepository->findOneBySlug('homepage'); The test would look like this: $mockPageRepository->expects($this->any()) ->method('findOneBySlug') ->will($this->returnValue(new Page())); But it doesn't work -- PHPUnit doesn't spot the method call. The only

How to overload Python's __bool__ method? [duplicate]

我是研究僧i 提交于 2019-12-02 20:00:06
Possible Duplicate: defining “boolness” of a class in python I thought this should print "False", why is it printing "True"? >>> class Foo(object): ... def __bool__(self): ... return False ... >>> f = Foo() >>> if f: ... print "True" ... else: ... print "False" ... True >>> You should define __nonzero__() in Python 2.x. It was only renamed to __bool__() in Python 3.x. (The name __nonzero__() actually predates the introduction of the bool type by many years.) 来源: https://stackoverflow.com/questions/8909932/how-to-overload-pythons-bool-method

Magic methods __get and __set - example from ZCE

喜你入骨 提交于 2019-12-02 12:45:15
问题 class Magic { public $a = "A"; protected $b = array("a" => "A", "b" => "B", "c" => "C"); protected $c = array(1,2,3); public function __get($v) { echo "$v, "; return $this->b[$v]; } public function __set($var, $val) { echo "$var: $val,"; $this->$var = $val; } } $m = new Magic(); echo $m->a.", ".$m->b.", ".$m->c.","; $m->c = "CC"; echo $m->a.", ".$m->b.", ".$m->c.","; This is an example question (not from an actual exam) for ZCE. Can someone please explain to me... what's going on here, and

Magic methods __get and __set - example from ZCE

时光总嘲笑我的痴心妄想 提交于 2019-12-02 07:04:05
class Magic { public $a = "A"; protected $b = array("a" => "A", "b" => "B", "c" => "C"); protected $c = array(1,2,3); public function __get($v) { echo "$v, "; return $this->b[$v]; } public function __set($var, $val) { echo "$var: $val,"; $this->$var = $val; } } $m = new Magic(); echo $m->a.", ".$m->b.", ".$m->c.","; $m->c = "CC"; echo $m->a.", ".$m->b.", ".$m->c.","; This is an example question (not from an actual exam) for ZCE. Can someone please explain to me... what's going on here, and why the answer is... not at all what I expected? b, c, A, B, C,c: CC,b, c, A, B, C, then… what do you

Using __set with arrays solved, but why?

人走茶凉 提交于 2019-12-01 03:10:15
问题 Having done a bit of research, I eventually came across the answer to a question I was soon to ask here anyways; How do you work with arrays via the __get and __set magic methods in PHP? Whenever I was trying to set a value using something like $object->foo['bar'] = 42; it seemed to silently discard it. Anyways, the answer is simple; The __get method simply needs to return by reference. And after tossing an ampersand in front of it, sure enough it works. My question actually, is why? I can't

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

送分小仙女□ 提交于 2019-12-01 01:50:37
I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs , should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my attempts have been unsuccessful. Here's an SSCCE that should make isinstance return False in all cases but

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

本小妞迷上赌 提交于 2019-11-30 20:26:12
问题 I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object ( tuple in my case) when in reality it is not even a derivation of that. In order to achieve this, I plan to overwrite the __isinstance__ method which, according to the docs, should do exactly what I desire. However, it appears that I didn't understand how do to do that exactly, because my

Why PHP uses static methods in object context?

≯℡__Kan透↙ 提交于 2019-11-30 11:35:24
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 static method ? Note : I run PHP 5.4.4, error reporting to E_ALL . Fenton To call a static method, you don

Triggering __call() in PHP even when method exists

大城市里の小女人 提交于 2019-11-30 06:38:39
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 ). Ionuț G. Stan How about just make all your other methods protected, and proxy them through __callStatic? namespace test\foo; class A { public

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

笑着哭i 提交于 2019-11-30 05:07:25
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 that all of them ran faster if I used the sugary forms ( == , != , < , etc.). Why is this? I assumed the