magic-methods

Defining magic methods on classes

我的梦境 提交于 2019-12-10 16:26:59
问题 I would like to define a single object that can be iterated over without having to create a class and then an instance. Something like this: class Thing(object): stuff = ["foo", "bar", "baz"] @classmethod def __iter__(cls): return iter(cls.stuff) for thing in Thing: print thing However this doesn't actually work. Is there any way to do this? 回答1: What Ashwini correctly suggested in his comment is the following. This works in Python 2. class ThingType(type): __stuff__ = ["foo", "bar", "baz"]

php 5.1.6 magic __toString method

痴心易碎 提交于 2019-12-10 14:52:50
问题 In codeigniter Im trying to use this plugin which requires I implement a toString method in my models. My toString method simply does public function __toString() { return (string)$this->name; } On my local machine with php 5.3 everything works just fine but on the production server with php 5.1.6 it shows "Object id#48" where the value of the name property of that object should appear..... I found something about the problem here but I still dont understand... How can I fix this? 回答1:

PHP __get __set methods

旧时模样 提交于 2019-12-10 13:52:53
问题 class Dog { protected $bark = 'woof!'; public function __get($key) { if (isset($this->$key)) { return $this->$key; } } public function __set($key, $val) { if (isset($this->$key)) { $this->$key = $val; } } } What is the point of using these functions. if i can use $dog = new Dog(); $dog->bark = 'woofy'; echo $dog->bark; Why would I bother declaring 'bark' as protected ? Do the __get() and __set() methods in this case effectively make 'bark' public? 回答1: In this case, they do make $this->bark

Php __get and __set magic methods - why do we need those here?

不问归期 提交于 2019-12-10 13:03:30
问题 On Zend Quick Start Guide here http://framework.zend.com/manual/en/learning.quickstart.create-model.html we can see: class Application_Model_Guestbook { protected $_comment; protected $_created; protected $_email; protected $_id; public function __set($name, $value); public function __get($name); public function setComment($text); public function getComment(); ... I normally create my getters and setters without any magic method. I've seen this on the quick guide, and I don't understand why

Built-in magic variable names/attributes

℡╲_俬逩灬. 提交于 2019-12-09 15:16:13
问题 Background : For those not familiar with it, Sublime Text (and TextMate) provides syntax highlighting and other features through scopes which are defined by .tmLanguage language definition files, basically a bunch of regexes to identify various constructs in a given language, such as function definitions, various types of strings, reserved words, etc. I'm the maintainer of the Python Improved package (available via Package Control if you're interested) that aims to be a better language

PHP - Zend say avoid Magic Methods?

╄→гoц情女王★ 提交于 2019-12-09 09:25:39
问题 I was reading this page - http://deaduseful.com/blog/posts/50-php-optimisation-tips-revisited And one of the recommendations was to avoid using Magic Methods, cited from a Zend Performance PDF which gives no reason for its recommendation to avoid them. After some Google searching (and winding up here to an unrelated question) I wondered if anyone had any reccomendations on that front? I use __get() alot in my code, usually to save variables that I don't always use e.g. I may have a table with

How to make inline array initialization work like e.g. Dictionary initialization?

匆匆过客 提交于 2019-12-09 07:53:46
问题 Why is it possible to initialize a Dictionary<T1,T2> like this: var dict = new Dictionary<string,int>() { { "key1", 1 }, { "key2", 2 } }; ...but not to initialize, say, an array of KeyValuePair<T1,T2> objects in exactly the same way: var kvps = new KeyValuePair<string,int>[] { { "key1", 1 }, { "key2", 2 } }; // compiler error: "Array initializers can only be used in a variable // or field initializer. Try using a new expression instead." I realize that I could make the second example work by

informing interface methods are implemented via __call?

柔情痞子 提交于 2019-12-08 16:37:49
问题 I have an interface that declares the implementation needs methods such as find, findOrFail etc, basically Laravel eloquent methods. I declare these methods in the interface because not everything that implements the interface will extend eloquent so I declare them in the interface so my app always knows the methods are going to be there. What I want to know is, other than having a bunch of public function find($id){return parent::find($id)} type methods in the models that do extend the

Return null by reference via __get()

≡放荡痞女 提交于 2019-12-07 05:53:50
问题 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(

PHP: Detecting when a variables value has been changed

非 Y 不嫁゛ 提交于 2019-12-07 03:04:09
问题 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