Magic methods are implicitly invoked by a programming language when some event or language construct is used.
Questions tagged [magic-methods]
545 questions
12
votes
2 answers
informing interface methods are implemented via __call?
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…
Hailwood
- 79,753
- 103
- 257
- 412
11
votes
2 answers
Built-in magic variable names/attributes
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…
MattDMo
- 90,104
- 20
- 213
- 210
11
votes
2 answers
Which special methods bypasses __getattribute__ in Python?
In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass.
The docs mention special methods such as…
porgarmingduod
- 7,070
- 7
- 41
- 76
10
votes
1 answer
PHP magic methods example
I have this question from the Zend PHP study guide and can't find a proper explanation...
"A", "b"=>"B", "c"=>"C");
protected $c = array(1,2,3);
…
Jan Petzold
- 1,543
- 15
- 24
10
votes
1 answer
How to use is_callable with __call?
I use PHP 5.3, which introduces closures. Because I now have closures available throughout my app (and framework), I use is_callable to see what kind of handler $callback is.
If $callback is_callable, I know enough and use that…
Rudie
- 46,504
- 37
- 126
- 167
10
votes
5 answers
Is there any case where len(someObj) does not call someObj's __len__ function?
Is there any case where len(someObj) does not call someObj's __len__ function?
I recently replaced the former with the latter in a (sucessful) effort to speed up some code. I want to make sure there's not some edge case somewhere where len(someObj)…
David Locke
- 16,816
- 8
- 30
- 53
10
votes
3 answers
Python equivalent of PHPs __call() magic method?
In PHP, I can do something like this:
class MyClass
{
function __call($name, $args)
{
print('you tried to call a the method named: ' . $name);
}
}
$Obj = new MyClass();
$Obj->nonexistant_method(); // prints "you tried to call a method…
Keith Palmer Jr.
- 26,497
- 16
- 62
- 103
10
votes
3 answers
PHP - Zend say avoid Magic Methods?
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…
Rob
- 143
- 1
- 6
10
votes
1 answer
Is any magic method called on an object in a list during join()?
Joining a list containing an object - is there any magic method I could set to convert the object to a string before join fails?
', '.join([…, Obj, …])
I tried __str__ and __repr__ but neither did work
Hoffmann
- 911
- 7
- 24
10
votes
5 answers
PHP's magic method __call on subclasses
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…
nickf
- 499,078
- 194
- 614
- 709
9
votes
1 answer
Using PDO::FETCH_CLASS with Magic Methods
I have a class that uses magic methods to store properties. Here is a simplified example:
class Foo {
protected $props;
public function __construct(array $props = array()) {
$this->props = $props;
}
public function…
FtDRbwLXw6
- 25,206
- 12
- 61
- 102
9
votes
3 answers
Why doesn't Python have a hybrid getattr + __getitem__ built in?
I have methods that accept dicts or other objects and the names of "fields" to fetch from those objects. If the object is a dict then the method uses __getitem__ to retrieve the named key, or else it uses getattr to retrieve the named attribute.…
Kirk Strauser
- 27,753
- 5
- 45
- 62
9
votes
4 answers
__callStatic(), call_user_func_array(), references, and PHP 5.3.1
I've been reading around on SO, and elsewhere, however I can't seem to find anything conclusive.
Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below?…
Dan Lugg
- 18,516
- 18
- 99
- 168
9
votes
5 answers
Increment on "__toString"
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…
Baba
- 89,415
- 27
- 158
- 212
9
votes
2 answers
PHP - __call all the time
Say we have a class with several protected and/or public methods.
I need to perform a check each time a method is called. I could do that check each time i call a method :
class Object
{
// Methods
}
$o = new Object();
if($mayAccess)…
Virus721
- 7,156
- 8
- 49
- 110