Magic methods are implicitly invoked by a programming language when some event or language construct is used.
Questions tagged [magic-methods]
616 questions
-1
votes
1 answer
__set magic method for sql select statements
In here I'm going to create a class to work with SQL select using PHP __set magic method.My var_dump array return array(0) { } In the browser.What is the error i have did ?
class helper{
public function __set($table,$id){
…

Mr PHP
- 33
- 7
-1
votes
3 answers
Codeigniter wont let me call model from model , is this true
So I've been trying for hours to get this to work but couldn't!
I have a base model that loads other models. These other models during their construct function reference some variables from the base model. For example:
User model:
Check if the user…

Zalaboza
- 8,899
- 16
- 77
- 142
-1
votes
3 answers
__get and __set magic methods not accessible
I have a class 'base' and a class 'loader', which looks like this.
class base {
protected $attributes = Array();
public $load = null;
function __construct() {
$this->load = loader::getInstance();
echo…

Abhishek Saha
- 2,564
- 1
- 19
- 29
-2
votes
1 answer
Object to return property?
I have a class called Type:
class Type { ... }
Which has a property named $value:
class Type {
protected $value;
//More code here...
}
I wish that when I attempt to use a function, when I pass the object, the value of $obj->value will be…

Madara's Ghost
- 172,118
- 50
- 264
- 308
-2
votes
2 answers
Python data model / internal methods
I would like to understand the data model in python (3.8.2), so maybe someone can explain in simple words, why every basic data type is a class-object and has so many attributes and functions.
Consider the following code:
num = 1337
type(num) #…

Marcellvs
- 391
- 1
- 3
- 15
-2
votes
1 answer
Are dunders methods or functions?
I'm a bit confused with the use of methods like __iter__() and __next__() (I suppose they are called dunders).
I was trying to understand iterators and iterables and wrote this code:
x = (1, 2, 3, 4, 5, 6)
try:
y = x.__iter__()
while 1:
…

newBjava
- 7
- 2
-2
votes
2 answers
Magic methods on other programming languages
Python has such methods as __add__, __mul__, __cmp__ and so on (called magic methods), which are used as a class methods and can give a different meaning to adding(+), multiplying(*), comparing(==), ... two instances of a class. My question is do…

Aven Desta
- 2,114
- 12
- 27
-2
votes
1 answer
Does __destruct run by itslef or do I need to use unset() or register_shutdown_function() in order for it to work
a) Does __destruct run every single time a code is processed, just by itself
b) Or, no, you need to use unset($objectName) in order for it to run (another option is register_shutdown_function() ).
c) Are there any other aspects related to this?…

A Badger
- 11
- 2
-2
votes
1 answer
Deletion of a PHP object; also unset() and __destruct
Are these correct:
Any PHP object always gets deleted after it is run (like the code of it).
unset($objectName) just stimulates what would happen anyway. The deletion will be a little bit faster (from RAM memory) but in the end it is exactly the…

Andy Rogers
- 33
- 5
-2
votes
1 answer
Magic method __str__ is not called on str call
I'm the creator of module-wrapper library and one of the authors of aioify library. The problem arises in magic method wrapping inside module-wrapper library (I call module_wrapper.wrap from aioify.aioify, but that doesn't matter).
I have the…

rominf
- 2,719
- 3
- 21
- 39
-2
votes
1 answer
'__neg__' implementation. i am learning magic methods in python
this is a polynomial class
class Polynomial(object):
def __init__(self, polynomial):
self.p = tuple(polynomial)
def get_polynomial(self):
return self.p
def __neg__(self):
return tuple([tuple([-i[j] if j==0 else i[j] for j in…

RayMan
- 1
- 1
-2
votes
1 answer
Symfony, how get current user (FOS) in entity, unused __construct()?
I have an entity Book. Title of this Book is stored in different languages (the entity Book is associated with BookTranslates as OneToMane).
The entity Book is used as a form field in several places (like select). For this, I need to set…

Сергей Сиротин
- 1
- 3
-3
votes
1 answer
Why are these magical methods not working?
The result after running this code is below, can someone explain why isn't name passed properly so that it would say "Icefeet is years old"
Am I missing something here?
is years old

Cass Hern
- 3
- 3
-3
votes
4 answers
I am attempting to print only a selected amount of Pi, it returns with an error of "Decimal has no attribute: __getitem__
def pi():
prompt=">>> "
print "\nWARNING: Pi may take some time to be calculated and may not always be correct beyond 100 digits."
print "\nShow Pi to what digit?"
n=raw_input(prompt)
from decimal import Decimal, localcontext
…

Patrick Cook
- 458
- 12
- 27
-3
votes
1 answer
__set in constructor?
Is the magic method __set called when setting properties in __construct?
class MyClass
{
public function __construct()
{
$this->property = 'something';
}
public function __set($name,$value)
{
$this->{$name} =…

olanod
- 30,306
- 7
- 46
- 75