magic-methods

PHP's magic method __call on subclasses

此生再无相见时 提交于 2019-12-06 21:44:35
问题 My situation is best described with a bit of code: class Foo { function bar () { echo "called Foo::bar()"; } } class SubFoo extends Foo { function __call($func) { if ($func == "bar") { echo "intercepted bar()!"; } } } $subFoo = new SubFoo(); // what actually happens: $subFoo->bar(); // "called Foo:bar()" // what would be nice: $subFoo->bar(); // "intercepted bar()!" I know I can get this to work by redefining bar() (and all the other relevant methods) in the sub-class, but for my purposes, it

In python, how do I create two index slicing for my own matrix class?

安稳与你 提交于 2019-12-06 09:28:02
问题 I am trying to write my own matrix class in python, just for testing purposes. In reality, this matrix class is in c++ and I am using SWIG to interface between the two. However, for this question, it might be simpler to consider a pure python implementation of this matrix class. I want to be able to call this matrix class and use two-indexed slicing. For example, after we create 4x4 matrix of ones, >>> A = Matrix(4,4,1) I want to be able to get the sub 2x2 matrix: >>>> A[1:2,1:2] I've heard

__getitem__, __setitem__ multiple keys Python

两盒软妹~` 提交于 2019-12-05 18:39:28
I am trying to create a class that stores data in a local buffer as well as acts as an interface to a database. I've got following code: class Table(object): def __init__(self, tableName, **columnDict): self.tableName = tableName self.columns = {} self.types = {} self.columns['id'] = [] self.types['id'] = 'INT PRIMARY KEY NOT NULL' for name in columnDict: self.columns[name] = [] self.types[name] = columnDict[name] def updateBufferRow(self, index, updateDict): for key in updateDict: self.columns[key][index] = updateDict[key] def getBufferRow(self, index): row = {} for key in self.columns: row

Return null by reference via __get()

耗尽温柔 提交于 2019-12-05 10:13:15
Quick specs: PHP 5.3 error_reporting(-1) // the highest I'm using the __get() by reference trick to magically access arbitrarily deep array elements in an object. Quick example: public function &__get($key){ return isset($this->_data[$key]) ? $this->_data[$key] : null; } This doesn't work as when the $key isn't set, it tries to return null by reference, which of course throws Only variable references should be returned by reference ... I tried modifying it as follows: public function &__get($key){ $null = null; return isset($this->_data[$key]) ? $this->_data[$key] : $null; } Still doesn't work

PHP: Detecting when a variables value has been changed

微笑、不失礼 提交于 2019-12-05 06:10:24
I was wondering if there is a way to add something like a change listener to a variable. The simplest example of what I mean would work something along these lines; // Start with a variable $variable = "some value"; // Define a listener function myChangeListener($variable) { // encode with json_encode and send in cookie } // Add my listener to the variable addListenerToVariable(&$variable, 'myChangeListener'); // Change the variables value, triggering my listener $variable = "new value"; Now you're probably asking why in the world I would even need to bother with this approach, why not just

PHP's magic method __call on subclasses

时光怂恿深爱的人放手 提交于 2019-12-05 02:37:43
My situation is best described with a bit of code: class Foo { function bar () { echo "called Foo::bar()"; } } class SubFoo extends Foo { function __call($func) { if ($func == "bar") { echo "intercepted bar()!"; } } } $subFoo = new SubFoo(); // what actually happens: $subFoo->bar(); // "called Foo:bar()" // what would be nice: $subFoo->bar(); // "intercepted bar()!" I know I can get this to work by redefining bar() (and all the other relevant methods) in the sub-class, but for my purposes, it'd be nice if the __call function could handle them. It'd just make things a lot neater and more

PHP magic methods example

陌路散爱 提交于 2019-12-04 18:19:06
问题 I have this question from the Zend PHP study guide and can't find a proper explanation... <?php 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; ?> According to the guide, solution shall be "

In python, how do I create two index slicing for my own matrix class?

廉价感情. 提交于 2019-12-04 17:26:45
I am trying to write my own matrix class in python, just for testing purposes. In reality, this matrix class is in c++ and I am using SWIG to interface between the two. However, for this question, it might be simpler to consider a pure python implementation of this matrix class. I want to be able to call this matrix class and use two-indexed slicing. For example, after we create 4x4 matrix of ones, >>> A = Matrix(4,4,1) I want to be able to get the sub 2x2 matrix: >>>> A[1:2,1:2] I've heard of the __getslice__ method, but this seems like it only allows single slicing, e.g. A[1:2] . So how can

Emulate public/private properties with __get() and __set()?

流过昼夜 提交于 2019-12-04 16:28:46
I was writing a class that uses __get() and __set() to store and retrieve array elements in a master array. I had a check to make some elements ungettable, basically to re-create private properties. I noticed that it seemed that __get intercepts all calls to class properties. This sucks for me, because I wanted to have a variable private to the outside world ( unavailable via get ), but I was trying to access it by directly referencing the master array from within the class. Of course, the master array is not in the whitelist of gettable properties :( Is there a way I can emulate public and

Check if a property exists on magically set properties

筅森魡賤 提交于 2019-12-04 10:26:07
问题 There is a lot of SO questions about the subject, notably this one, but it does not help me. There is an ambiguity between property_exists and isset so before asking my question, I'm going to pointing it out: property_exists property_exists checks if an object contains a property without looking at its value, it only looks at its visibility. So in the following example: <?php class testA { private $a = null; } class testB extends testA { } $test = new testA(); echo var_dump(property_exists(