magic-methods

Overriding __hex__ in python 3?

一世执手 提交于 2019-12-19 12:28:22
问题 I have the following class: from __future__ import print_function class Proxy(object): __slots__ = ['_value'] def __init__(self, obj): self._value = obj def __hex__(self): return hex(self._value) print(hex(42)) print(hex(Proxy(42))) in Python 2.7 this prints (py2.7) c:\hextest> python hextest.py 0x2a 0x2a but in Py3.8 this raises an exception: (py3.8) c:\hextest> python hextest.py 0x2a Traceback (most recent call last): File "hextest.py", line 14, in <module> print(hex(Proxy(42))) TypeError:

How to implement __isset() magic method in PHP?

你。 提交于 2019-12-19 05:22:20
问题 I'm trying to make functions like empty() and isset() work with data returned by methods. What I have so far: abstract class FooBase{ public function __isset($name){ $getter = 'get'.ucfirst($name); if(method_exists($this, $getter)) return isset($this->$getter()); // not working :( // Fatal error: Can't use method return value in write context } public function __get($name){ $getter = 'get'.ucfirst($name); if(method_exists($this, $getter)) return $this->$getter(); } public function __set($name

How to define model fields with idiorm/granada without breaking the ORM functionality?

房东的猫 提交于 2019-12-14 04:11:32
问题 The PHP orm Granada based on Idiorm works the following way to retrieve fields from database: class ORM { ... public function __get($key) { return $this->get($key); } } class ORMWrapper extends ORM { ... public function get($key) { if (method_exists($this, 'get_' . $key)) { return $this->{'get_' . $key}(); } elseif (array_key_exists($key, $this->_data)) { return $this->_data[$key]; } elseif (array_key_exists($key, $this->ignore)) { return $this->ignore[$key]; } // and so on ... } My problem

How to override a magic method for the current script without a class?

假装没事ソ 提交于 2019-12-13 08:37:00
问题 How to override a magic method for the current script? def __setattr__(name, value): __dict__[name] = value print("asd") if name == 'b': print(__dict__[name]) if __name__ == '__main__': a = 3 b = 4 b = 5 In above, for example, I expect assignments to b to call __setattr__ but they don't. What am I missing? 回答1: __setattr__ only applies to assignments of the form a.b = c , which translates to type(a).__setattr__(b, c) . Simple name assignments are a fundamental operation of the language itself

Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate]

我的梦境 提交于 2019-12-13 00:47:32
问题 This question already has answers here : Why won't dynamically adding a `__call__` method to an instance work? (2 answers) Closed 11 months ago . I have a wrapper class similar to this (strongly simplified) example: class wrap(object): def __init__(self): self._data = range(10) def __getitem__(self, key): return self._data.__getitem__(key) I can use it like this: w = wrap() print w[2] # yields "2" I thought I could optimize and get rid of one function call by changing to this: class wrap

PHP: Overriding parent methods with __call

匆匆过客 提交于 2019-12-12 18:05:04
问题 I'd like to hide parent methods in some manner so that a child class's __call magic method is invoked for methods defined on the parent. For example: class C { function foo() { echo "foo\n"; } } class D extends C { function __call( $method, $args ) { echo "called $name\n"; } } $d = new D(); $d->foo(); // desired result: called foo // actual result: foo I've checked into rename_function and override_function but those don't work for methods. ReflectionMethod has a setAccessible method I tried,

How have getters and setters for every property of class?

泄露秘密 提交于 2019-12-11 20:47:31
问题 Can't we use php magic getter setter for doctrine2 entity classes instead of making getter setter for every property of class? This is my entity class written for doctrine2 to map with table in database. <?php namespace Entities; use Doctrine\ORM\Mapping AS ORM; /** * @ORM\Entity */ class blogs { /** * @ORM\Id * @ORM\Column(type="integer", length=11) * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(type="string", length=1024, nullable=true) */ private $url; /** * @ORM

&__get() issues, again. Major frustration is afoot

末鹿安然 提交于 2019-12-11 18:19:19
问题 Alrighty, I'm getting quite frustrated, namely because I thought I had this issue solved, or had accomplished this successfully before. Quick preliminary: PHP 5.3.6. Error reporting cranked to 11. ( -1 actually; future safe, all errors/notices ) I have a class, it aggregates request parameters. For giggles here is a stripped down version: class My_Request{ private $_data = array(); public function __construct(Array $params, Array $session){ $this->_data['params'] = $params; $this->_data[

PHPUnit mock - call parent __get/__set/__isset

痞子三分冷 提交于 2019-12-11 06:07:23
问题 I am trying to mock a class that has inherited magic methods, but they are not getting implemented, and I have no idea how to fix it. Here's the code: <?php use PHPUnit\Framework\TestCase; class AnnoyingTestCase extends TestCase { public function testNoAttributes() { $mock = $this->createMock(Child::class); $mock->attribute = 'value'; $this->assertEquals($mock->attribute, null); } } class Base { private $attributes = []; public function __set($name, $value) { $this->attributes[$name] = $value

CakePHP 2.0.4 - findBy magic methods with conditions

女生的网名这么多〃 提交于 2019-12-11 03:25:41
问题 I am trying to build a small cms to test CakePHP 2.x In my PagesController (for displaying single sites), I use this code: $page = $this->Page->findByNavtitle($name, array( 'conditions' => array( 'Page.visible' => '1', ), ) ); The result should only set when the record is marked as visible. But this codeblock throws an error. The API describes, that just one parameter is allowed in these findBy magic methods. How can I get the result with conditions? 回答1: You cannot add conditions to findBy