magic-methods

__callStatic(): instantiating objects from static context?

情到浓时终转凉″ 提交于 2019-12-23 07:00:16
问题 I am confused about how "static" and "dynamic" functions and objects in PHP work together especially with regards to __callStatic(). How __callStatic() works: You can have a normal class MyClass, where within the class you can put a static function called __callStatic(), which gets called only when MyClass doesn't have a static function by the name you want. i.e. I call MyClass::newFunction(); newFunction() is called statically but MyClass does not have it declared. So, then __callStatic()

__getitem__, __setitem__ multiple keys Python

好久不见. 提交于 2019-12-22 10:59:24
问题 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

Confusing class and method call in OpenCart

瘦欲@ 提交于 2019-12-22 06:02:34
问题 I have a framework (OpenCart) Controller class (like: catalog/controller/product/product.php) the code looks like: class ControllerProductProduct extends Controller { public function index() { //some code $this->response->setOutput($this->render()); //some more code } } there is an expression like $this->response->setOutput($this->render()); . I know what this expression is used for, but I am pretty confused in how it works. $this refers to current class i.e. ControllerProductProduct , it

What is the real purpose of magic method __set_state in PHP?

给你一囗甜甜゛ 提交于 2019-12-22 05:13:44
问题 I'm learning about magic methods, reading every site, taking every example, but nothing makes sense to me. Examples like this: class A { public $var1; public $var2; public static function __set_state($an_array) // As of PHP 5.1.0 { $obj = new A; $obj->var1 = $an_array['var1']; $obj->var2 = $an_array['var2']; return $obj; } } $a = new A; $a->var1 = 5; $a->var2 = 'foo'; eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array( // 'var1' => 5, // 'var2' => 'foo', // )); var_dump(

Increment on “__toString”

别来无恙 提交于 2019-12-21 09:00:44
问题 I am not sure what the title should be, but the code should explain it better: class Group { private $number = 20; public function __toString() { return "$this->number"; } } $number = new Group(); echo $number, PHP_EOL; echo ++ $number, PHP_EOL; echo PHP_EOL; $number = "20"; echo $number, PHP_EOL; echo ++ $number, PHP_EOL; echo PHP_EOL; $number = 20; echo $number, PHP_EOL; echo ++ $number, PHP_EOL; Output: 20 20 <--- Expected 21 20 21 20 21 Any idea why I got 20 instead of 21 ? Even then the

Magic Methods in JavaScript [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 07:26:15
问题 This question already has answers here : JavaScript getter for all properties (7 answers) Closed 5 years ago . Are the any magic(al) methods under JavaScript ? I'm explicitly interested in setters , getters and callables (functions/methods), like those we have in PHP : __set , __get and __call && __callStatic . I was googling and searching StackOverflow, but no results. Please, if anyone is informed - THANKS! 回答1: see this: JavaScript's equivalent to PHP's __get() magic method JavaScript

Custom double star operator for a class?

天涯浪子 提交于 2019-12-21 04:03:03
问题 How does one implement custom double star operator ( ** ) for unpacking, similar to how __iter__ works with single star operator ( * )? For example: class PlayerManager(object): def __init__(self, players=None): self.players = players or [] # Made up method to support ** operator def __dict_iter__(self): for player in self.players: yield get_steamid(player), player def print_players(**players): print(players) player_manager = PlayerManager([list, of, players]) print_players(**player_manager)

How to write a static python getitem method?

我的梦境 提交于 2019-12-21 03:54:17
问题 What do I need to change to make this work? class A: @staticmethod def __getitem__(val): return "It works" print A[0] Note that I am calling the __getitem__ method on the type A . 回答1: When an object is indexed, the special method __getitem__ is looked for first in the object's class. A class itself is an object, and the class of a class is usually type . So to override __getitem__ for a class, you can redefine its metaclass (to make it a subclass of type ): class MetaA(type): def __getitem__

Python class method chaining

社会主义新天地 提交于 2019-12-20 12:43:15
问题 To avoid getting lost in architectural decisions, I'll ask this with an analogous example: lets say I wanted a Python class pattern like this: queue = TaskQueue(broker_conn) queue.region("DFW").task(fn, "some arg") The question here is how do I get a design a class such that certain methods can be "chained" in this fashion. task() would require access to the queue class instance attributes and the operations of task depends on the output of region() . I see SQLalchemy does this (see below)

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

人走茶凉 提交于 2019-12-20 10:26:40
问题 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(