Métodos mágicos

Métodos mágicos são métodos especiais que sobrescrever o comportamento padrão do PHP quando certas operações são realizadas em um objeto.

Cuidado

Todos os métodos prefixados com __ são reservados pelo PHP. Portanto, não é recomendado utilizar nomes de métodos com esse prefixo a não ser para sobrescrever o comportamento do PHP.

Os seguintes nomes de métodos são considerados mágicos: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone() e __debugInfo()

Aviso

Os métodos acima, com exceção de __construct(), __destruct() e __clone(), precisam ser declarados como public, de outra forma um E_WARNING é emitido. Anteriormente ao PHP 8.0.0, nenhum diagnóstico era emitido para os métodos mágicos __sleep(), __wakeup(), __serialize(), __unserialize() e __set_state().

Aviso

Se tipos forem utilizados na declaração de métodos mágicos, eles precisam ser idênticos às assinaturas previstas aqui. Senão, um erro fatal é lançado. Anteriormente ao PHP 8.0.0, nenhum diagnóstico era emitido. Entretanto, __construct() e __destruct() não devem declarar um tipo de retorno, senão um erro fatal é lançado.

__sleep() e __wakeup()

public __sleep(): array
public __wakeup(): void

serialize() checa se sua classe tem uma função com o nome mágico __sleep(). Se houver, a função é executada antes de qualquer serialização. Ela pode limpar o objeto e deve retornar um array com os nomes de todas as variáveis do objeto que devem ser serializadas. Se o método não retornar nada, então null é serializado e um E_NOTICE disparado.

Nota:

Não é possível que __sleep() retorne nomes de propriedades privadas da classe pai. Fazer isso causará um erro de nível E_NOTICE. Como alternativa, utilize __serialize().

Nota:

Desde o PHP 8.0.0, retornar um valor que não seja um array de __sleep() gera um warning. Anteriormente gerava um aviso.

O intuito do método __sleep() é enviar dados pendentes ou realizar tarefas de limpeza. Além disso, a função é útil se tiver objetos muito grandes que não precisem ser completamente salvos.

Ao mesmo tempo, unserialize() checa pela presença da função com o nome mágico __wakeup(). Se presente, essa função pode reconstruir qualquer recurso que o objeto possa ter.

O intuito do método __wakeup() é reestabelecer qualquer conexão com banco de dados que podem ter sido perdidas durante a serialização, e realizar outras tarefas de reinicialização.

Exemplo #1 Sleep e wakeup

<?php
class Connection
{
protected
$link;
private
$dsn, $username, $password;

public function
__construct($dsn, $username, $password)
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
$this->connect();
}

private function
connect()
{
$this->link = new PDO($this->dsn, $this->username, $this->password);
}

public function
__sleep()
{
return array(
'dsn', 'username', 'password');
}

public function
__wakeup()
{
$this->connect();
}
}
?>

__serialize() e __unserialize()

public __serialize(): array
public __unserialize(array $data): void

serialize() verifica se a classe contém uma função com o nome mágico __serialize(). Se sim, essa função é executada antes de qualquer serialização. Ela precisa construir e retornar um array associativo de chaves-valores que representam a forma serializada do objeto. Se o array não for retornado então um erro TypeError será lançado.

Nota:

Se ambos __serialize() e __sleep() estiverem definidos no mesmo objeto, somente __serialize() será chamado. __sleep() será ignorado. Se o objeto implementa a interface Serializable, o método serialize() da interface será ignorado e o método mágico __serialize() será utilizado.

O uso pretendido de __serialize() é definir uma representação arbitrária, amigável, da representação do objeto. Elementos do array podem corresponder a propriedades do objeto diretamente, mas isso não é obrigatório.

Inversamente, unserialize() verifica a presença da função mágica __unserialize(). Se presente, essa função será chamada com o array retornado de __serialize(). Ela poderá, então, restaurar as propriedades do objeto a partir do array.

Nota:

Se ambos __unserialize() e __wakeup() estiverem definidos, somente __unserialize() será chamado, e __wakeup() será ignorado.

Nota:

Esse recurso está disponível desde o PHP 7.4.0.

Exemplo #2 Serialize e unserialize

<?php
class Connection
{
protected
$link;
private
$dsn, $username, $password;

public function
__construct($dsn, $username, $password)
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
$this->connect();
}

private function
connect()
{
$this->link = new PDO($this->dsn, $this->username, $this->password);
}

public function
__serialize(): array
{
return [
'dsn' => $this->dsn,
'user' => $this->username,
'pass' => $this->password,
];
}

public function
__unserialize(array $data): void
{
$this->dsn = $data['dsn'];
$this->username = $data['user'];
$this->password = $data['pass'];

$this->connect();
}
}
?>

__toString()

public __toString(): string

O método __toString() permite que uma classe decida como se comportar quando convertida para uma string. Por exemplo, o que echo $obj; irá imprimir.

Aviso

A partir do PHP 8.0.0, o valor de retorno segue as mesma semântica de tipo do PHP, significando que o valor será convertido para string se possível e se strict typing estiver desligado.

Um objeto Stringable não é aceito para uma declaração string se strict typing estiver ativo. Caso esse comportamento seja desejado, a declaração precisa indicar Stringable e string através de um tipo união.

A partir do PHP 8.0.0, quaisquer classe que contenha o método __toString() também implementa implicitamente a interface Stringable, e portanto passa os testes para essa interface. Implementar explicitamente essa interface é o recomendado.

No PHP 7.4, o valor retornado precisa ser uma string, senão um erro Error é lançado.

Anteriormente ao PHP 7.4.0, o valor retornado precisa ser uma string, senão um erro fatal E_RECOVERABLE_ERROR é emitido.

Aviso

Não era possível lançar uma exception de dentro de um método __toString() antes do PHP 7.4.0. Isso gera um erro fatal.

Exemplo #3 Exemplo Simples

<?php
// Declara uma classe simples
class TestClass
{
public
$foo;

public function
__construct($foo)
{
$this->foo = $foo;
}

public function
__toString()
{
return
$this->foo;
}
}

$class = new TestClass('Hello');
echo
$class;
?>

O exemplo acima produzirá:

Hello

__invoke()

__invoke( ...$values): mixed

O método __invoke() é chamado quando um script tenta chamar um objeto como uma função.

Exemplo #4 Usando __invoke()

<?php
class CallableClass
{
public function
__invoke($x)
{
var_dump($x);
}
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));
?>

O exemplo acima produzirá:

int(5)
bool(true)

Exemplo #5 Exemplo de __invoke()

<?php
class Sort
{
private
$key;

public function
__construct(string $key)
{
$this->key = $key;
}

public function
__invoke(array $a, array $b): int
{
return
$a[$this->key] <=> $b[$this->key];
}
}

$customers = [
[
'id' => 1, 'first_name' => 'John', 'last_name' => 'Do'],
[
'id' => 3, 'first_name' => 'Alice', 'last_name' => 'Gustav'],
[
'id' => 2, 'first_name' => 'Bob', 'last_name' => 'Filipe']
];

// Ordena os clientes pelo primeiro nome
usort($customers, new Sort('first_name'));
print_r($customers);

// Ordena os clientes pelo último nome
usort($customers, new Sort('last_name'));
print_r($customers);
?>

O exemplo acima produzirá:

Array
(
    [0] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )

    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )

    [2] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [first_name] => John
            [last_name] => Do
        )

    [1] => Array
        (
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        )

    [2] => Array
        (
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        )

)

__set_state()

static __set_state(array $properties): object

Esse método estático é chamado em classes exportadas por var_export().

O único parâmetro deste método é um array contendo propriedades exportadas no formato ['property' => value, ...].

Exemplo #6 Usando __set_state()

<?php

class A
{
public
$var1;
public
$var2;

public static function
__set_state($an_array)
{
$obj = new A;
$obj->var1 = $an_array['var1'];
$obj->var2 = $an_array['var2'];
return
$obj;
}
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

$b = var_export($a, true);
var_dump($b);
eval(
'$c = ' . $b . ';');
var_dump($c);
?>

O exemplo acima produzirá:

string(60) "A::__set_state(array(
   'var1' => 5,
   'var2' => 'foo',
))"
object(A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

Nota: Quando exportando um objeto, var_export() não verifica se __set_state() está implementado na classe do objeto, de forma que re-importar esses objetos falham com um erro Error na ausência de __set_state(). Isto afeta particularmente algumas classes internas. É responsabilidade do programador verificar se todos os objetos podem ser re-importados, ou seja, que todas as classes implementem __set_state().

__debugInfo()

__debugInfo(): array

Este método é chamado pela função var_dump() ao despejar um objeto para obter as propriedades que devem ser exibidas. Se este método não for definido em um objeto, todos as propriedades públicas, protegidas e provadas serão exibidas.

Exemplo #7 Utilizando o __debugInfo()

<?php
class C {
private
$prop;

public function
__construct($val) {
$this->prop = $val;
}

public function
__debugInfo() {
return [
'propSquared' => $this->prop ** 2,
];
}
}

var_dump(new C(42));
?>

O exemplo acima produzirá:

object(C)#1 (1) {
  ["propSquared"]=>
  int(1764)
}
add a note add a note

User Contributed Notes 30 notes

up
33
jon at webignition dot net
15 years ago
The __toString() method is extremely useful for converting class attribute names and values into common string representations of data (of which there are many choices). I mention this as previous references to __toString() refer only to debugging uses.

I have previously used the __toString() method in the following ways:

- representing a data-holding object as:
   - XML
   - raw POST data
   - a GET query string
   - header name:value pairs

- representing a custom mail object as an actual email (headers then body, all correctly represented)

When creating a class, consider what possible standard string representations are available and, of those, which would be the most relevant with respect to the purpose of the class.

Being able to represent data-holding objects in standardised string forms makes it much easier for your internal representations of data to be shared in an interoperable way with other applications.
up
17
jsnell at e-normous dot com
15 years ago
Be very careful to define __set_state() in classes which inherit from a parent using it, as the static __set_state() call will be called for any children.  If you are not careful, you will end up with an object of the wrong type.  Here is an example:

<?php
class A
{
    public
$var1;

    public static function
__set_state($an_array)
    {
       
$obj = new A;
       
$obj->var1 = $an_array['var1']; 
        return
$obj;
    }
}

class
B extends A {
}

$b = new B;
$b->var1 = 5;

eval(
'$new_b = ' . var_export($b, true) . ';');
var_dump($new_b);
/*
object(A)#2 (1) {
  ["var1"]=>
  int(5)
}
*/
?>
up
2
ctamayo at sitecrafting dot com
3 years ago
Due to a bug in PHP <= 7.3, overriding the __debugInfo() method from SPL classes is silently ignored.

<?php

class Debuggable extends ArrayObject {
  public function
__debugInfo() {
    return [
'special' => 'This should show up'];
  }
}

var_dump(new Debuggable());

// Expected output:
// object(Debuggable)#1 (1) {
//   ["special"]=>
//   string(19) "This should show up"
// }

// Actual output:
// object(Debuggable)#1 (1) {
//   ["storage":"ArrayObject":private]=>
//   array(0) {
//   }
// }

?>

Bug report: https://bugs.php.net/bug.php?id=69264
up
6
smiley at HELLOSPAMBOT dot chillerlan dot net
8 years ago
A simple API wrapper, using __call() and the PHP 5.6 "..." token.
http://php.net/manual/functions.arguments.php#functions.variable-arg-list

<?php
namespace Example;

use
Exception;
use
ReflectionClass;
use
SomeApiInterface;
use
SomeHttpClient;
use
SomeEndpointHandler;

/**
* Class SomeApiWrapper
*
* @method SomeEndpointHandler method1(MethodParams $param1)
* @method SomeEndpointHandler method2(MethodParams $param1, AuthParams $param2 = null)
* ...
* @method SomeEndpointHandler method42()
*/
class SomeApiWrapper{

   
/**
     * @var \SomeHttpClient
     */
   
private $httpClient;

   
/**
     * @var array
     */
   
private $methodMap = [];

   
/**
     * SomeApiWrapper constructor.
     */
   
public function __construct(){
       
$this->mapApiMethods();
       
$this->httpClient = new SomeHttpClient();
    }

   
/**
     * The API is flat and has ~ 150 endpoints, all of which take optional parameters
     * from up to 3 groups (method params, authentication, filters). Instead of
     * implementing the interface and adding countless stubs that have basically
     * the same signature, i just map its methods here and use __call().
     */
   
private function mapApiMethods(){
       
$reflectionClass = new ReflectionClass(SomeApiInterface::class);

        foreach(
$reflectionClass->getMethods() as $m){
           
$this->methodMap[] = $m->name;
        }
    }

   
/**
     * Thanks to the PHP 5.6+ "..." token, there's no hassle with the arguments anymore
     * (ugh, bad pun). Just hand the method parameters into the endpoint handler,
     * along with other mandatory params - type hints are your friends.
     *
     * It's magic!
     *
     * @param string $method
     * @param array  $arguments
     *
     * @return \SomeEndpointHandler
     * @throws \Exception
     */
   
public function __call($method, $arguments){

        if(
in_array($method, $this->methodMap)){
            return new
SomeEndpointHandler($this->httpClient, $method, ...$arguments);
        }

        throw new
Exception('Endpoint "'.$method.'" does not exist');
    }

}
up
10
daan dot broekhof at gmail dot com
12 years ago
Ever wondered why you can't throw exceptions from __toString()? Yeah me too.

Well now you can! This trick allows you to throw any type of exception from within a __toString(), with a full & correct backtrace.

How does it work? Well PHP __toString() handling is not as strict in every case: throwing an Exception from __toString() triggers a fatal E_ERROR, but returning a non-string value from a __toString() triggers a non-fatal E_RECOVERABLE_ERROR.
Add a little bookkeeping, and can circumvented this PHP deficiency!
(tested to work PHP 5.3+)

<?php

set_error_handler
(array('My_ToStringFixer', 'errorHandler'));
error_reporting(E_ALL | E_STRICT);

class
My_ToStringFixer
{
    protected static
$_toStringException;

    public static function
errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine)
    {
        if (isset(
self::$_toStringException))
        {
           
$exception = self::$_toStringException;
           
// Always unset '_toStringException', we don't want a straggler to be found later if something came between the setting and the error
           
self::$_toStringException = null;
            if (
preg_match('~^Method .*::__toString\(\) must return a string value$~', $errorMessage))
                throw
$exception;
        }
        return
false;
    }
   
    public static function
throwToStringException($exception)
    {
       
// Should not occur with prescribed usage, but in case of recursion: clean out exception, return a valid string, and weep
       
if (isset(self::$_toStringException))
        {
           
self::$_toStringException = null;
            return
'';
        }

       
self::$_toStringException = $exception;

        return
null;
    }
}

class
My_Class
{
    public function
doComplexStuff()
    {
        throw new
Exception('Oh noes!');
    }

    public function
__toString()
    {
        try
        {
           
// do your complex thing which might trigger an exception
           
return $this->doComplexStuff();
        }
        catch (
Exception $e)
        {
           
// The 'return' is required to trigger the trick
           
return My_ToStringFixer::throwToStringException($e);
        }
    }
}

$x = new My_Class();

try
{
    echo
$x;
}
catch (
Exception $e)
{
    echo
'Caught Exception! : '. $e;
}
?>
up
6
kguest at php dot net
6 years ago
__debugInfo  is also utilised when calling print_r on an object:

$ cat test.php
<?php
class FooQ {

     private
$bar = '';

     public function
__construct($val) {

        
$this->bar = $val;
     }

     public function
__debugInfo()
     {
         return [
'_bar' => $this->bar];
     }
}
$fooq = new FooQ("q");
print_r ($fooq);

$
php test.php
FooQ Object
(
    [
_bar] => q
)
$
up
6
dhuseby domain getback tld com
16 years ago
The above hint for using array_keys((array)$obj) got me investigating how to get __sleep to really work with object hierarchies.

With PHP 5.2.3, If you want to serialize an object that is part of an object hierarchy and you want to selectively serialize members (public, private, and protected) by manually specifying the array of members, there are a few simple rules for naming members that you must follow:

1. public members should be named using just their member name, like so:

<?php
class Foo {
    public
$bar;

    public function
__sleep() {
        return array(
"bar");
    }
}
?>

2. protected members should be named using "\0" . "*" . "\0" . member name, like so:

<?php
class Foo {
    protected
$bar;

    public function
__sleep() {
        return array(
"\0*\0bar");
    }
}
?>

3. private members should be named using "\0" . class name . "\0" . member name, like so:

<?php
class Foo {
    private
$bar;

    public function
__sleep() {
        return array(
"\0Foo\0bar");
    }
}
?>

So with this information let us serialize a class hierarchy correctly:

<?php

class Base {
    private
$foo = "foo_value";
    protected
$bar = "bar_value";

    public function
__sleep() {
        return array(
"\0Base\0foo", "\0*\0bar");
    }
}

class
Derived extends Base {
    public
$baz = "baz_value";
    private
$boo = "boo_value";

    public function
__sleep() {
       
// we have to merge our members with our parent's
       
return array_merge(array("baz", "\0Derived\0boo"), parent::__sleep());
    }
}

class
Leaf extends Derived {
    private
$qux = "qux_value";
    protected
$zaz = "zaz_value";
    public
$blah = "blah_value";

    public function
__sleep() {
       
// again, merge our members with our parent's
       
return array_merge(array("\0Leaf\0qux", "\0*\0zaz", "blah"), parent::__sleep());
    }
}

// test it
$test = new Leaf();
$s = serialize($test);
$test2 = unserialize($s);
echo
$s;
print_r($test);
print_r($test2);

?>

Now if you comment out all of the __sleep() functions and output the serialized string, you will see that the output doesn't change.  The most important part of course is that with the proper __sleep() functions, we can unserialize the string and get a properly set up object.

I hope this solves the mystery for everybody.  __sleep() does work, if you use it correctly :-)
up
4
ddavenport at newagedigital dot com
19 years ago
One of the principles of OOP is encapsulation--the idea that an object should handle its own data and no others'.  Asking base classes to take care of subclasses' data, esp considering that a class can't possibly know how many dozens of ways it will be extended, is irresponsible and dangerous.

Consider the following...

<?php
class SomeStupidStorageClass
{
  public function
getContents($pos, $len) { ...stuff... }
}

class
CryptedStorageClass extends SomeStupidStorageClass
{
  private
$decrypted_block;
  public function
getContents($pos, $len) { ...decrypt... }
}
?>

If SomeStupidStorageClass decided to serialize its subclasses' data as well as its own, a portion of what was once an encrypted thingie could be stored, in the clear, wherever the thingie was stored.  Obviously, CryptedStorageClass would never have chosen this...but it had to either know how to serialize its parent class's data without calling parent::_sleep(), or let the base class do what it wanted to.

Considering encapsulation again, no class should have to know how the parent handles its own private data.  And it certainly shouldn't have to worry that users will find a way to break access controls in the name of convenience.

If a class wants both to have private/protected data and to survive serialization, it should have its own __sleep() method which asks the parent to report its own fields and then adds to the list if applicable.  Like so....

<?php

class BetterClass
{
  private
$content;

  public function
__sleep()
  {
    return array(
'basedata1', 'basedata2');
  }

  public function
getContents() { ...stuff... }
}

class
BetterDerivedClass extends BetterClass
{
  private
$decrypted_block;

  public function
__sleep()
  {
    return
parent::__sleep();
  }

  public function
getContents() { ...decrypt... }
}

?>

The derived class has better control over its data, and we don't have to worry about something being stored that shouldn't be.
up
2
jeffxlevy at gmail dot com
18 years ago
Intriguing what happens when __sleep() and __wakeup() and sessions() are mixed. I had a hunch that, as session data is serialized, __sleep would be called when an object, or whatever, is stored in _SESSION. true. The same hunch applied when session_start() was called. Would __wakeup() be called? True. Very helpful, specifically as I'm building massive objects (well, lots of simple objects stored in sessions), and need lots of automated tasks (potentially) reloaded at "wakeup" time. (for instance, restarting a database session/connection).
up
0
vali dot dr at gmail dot com
3 years ago
It should be noted that if you unset a class typed property and then try to access it,  __get will be called. But it MUST return the original type.

https://wiki.php.net/rfc/typed_properties_v2#overloaded_properties
up
1
rayRO
17 years ago
If you use the Magical Method '__set()', be shure that the call of
<?php
$myobject
->test['myarray'] = 'data';
?>
will not appear!

For that u have to do it the fine way if you want to use __set Method ;)
<?php
$myobject
->test = array('myarray' => 'data');
?>

If a Variable is already set, the __set Magic Method already wont appear!

My first solution was to use a Caller Class.
With that, i ever knew which Module i currently use!
But who needs it... :]
There are quiet better solutions for this...
Here's the Code:

<?php
class Caller {
    public
$caller;
    public
$module;

    function
__call($funcname, $args = array()) {
       
$this->setModuleInformation();

        if (
is_object($this->caller) && function_exists('call_user_func_array'))
           
$return = call_user_func_array(array(&$this->caller, $funcname), $args);
        else
           
trigger_error("Call to Function with call_user_func_array failed", E_USER_ERROR);
       
       
$this->unsetModuleInformation();
        return
$return;
    }

    function
__construct($callerClassName = false, $callerModuleName = 'Webboard') {
        if (
$callerClassName == false)
           
trigger_error('No Classname', E_USER_ERROR);

       
$this->module = $callerModuleName;

        if (
class_exists($callerClassName))
           
$this->caller = new $callerClassName();
        else
           
trigger_error('Class not exists: \''.$callerClassName.'\'', E_USER_ERROR);

        if (
is_object($this->caller))
        {
           
$this->setModuleInformation();
            if (
method_exists($this->caller, '__init'))
               
$this->caller->__init();
           
$this->unsetModuleInformation();
        }
        else
           
trigger_error('Caller is no object!', E_USER_ERROR);
    }

    function
__destruct() {
       
$this->setModuleInformation();
        if (
method_exists($this->caller, '__deinit'))
           
$this->caller->__deinit();
       
$this->unsetModuleInformation();
    }

    function
__isset($isset) {
       
$this->setModuleInformation();
        if (
is_object($this->caller))
           
$return = isset($this->caller->{$isset});
        else
           
trigger_error('Caller is no object!', E_USER_ERROR);
       
$this->unsetModuleInformation();
        return
$return;
    }

    function
__unset($unset) {
       
$this->setModuleInformation();
        if (
is_object($this->caller)) {
            if (isset(
$this->caller->{$unset}))
                unset(
$this->caller->{$unset});
        }
        else
           
trigger_error('Caller is no object!', E_USER_ERROR);
       
$this->unsetModuleInformation();
    }

    function
__set($set, $val) {
       
$this->setModuleInformation();
        if (
is_object($this->caller))
           
$this->caller->{$set} = $val;
        else
           
trigger_error('Caller is no object!', E_USER_ERROR);
       
$this->unsetModuleInformation();
    }

    function
__get($get) {
       
$this->setModuleInformation();
        if (
is_object($this->caller)) {
            if (isset(
$this->caller->{$get}))
               
$return = $this->caller->{$get};
            else
               
$return = false;
        }
        else
           
trigger_error('Caller is no object!', E_USER_ERROR);
       
$this->unsetModuleInformation();
        return
$return;
    }
   
    function
setModuleInformation() {
       
$this->caller->module = $this->module;
    }

    function
unsetModuleInformation() {
       
$this->caller->module = NULL;
    }
}

// Well this can be a Config Class?
class Config {
    public
$module;

    public
$test;

    function
__construct()
    {
        print(
'Constructor will have no Module Information... Use __init() instead!<br />');
        print(
'--> '.print_r($this->module, 1).' <--');
        print(
'<br />');
        print(
'<br />');
       
$this->test = '123';
    }
   
    function
__init()
    {
        print(
'Using of __init()!<br />');
        print(
'--> '.print_r($this->module, 1).' <--');
        print(
'<br />');
        print(
'<br />');
    }
   
    function
testFunction($test = false)
    {
        if (
$test != false)
           
$this->test = $test;
    }
}

echo(
'<pre>');
$wow = new Caller('Config', 'Guestbook');
print_r($wow->test);
print(
'<br />');
print(
'<br />');
$wow->test = '456';
print_r($wow->test);
print(
'<br />');
print(
'<br />');
$wow->testFunction('789');
print_r($wow->test);
print(
'<br />');
print(
'<br />');
print_r($wow->module);
echo(
'</pre>');
?>

Outputs something Like:

Constructor will have no Module Information... Use __init() instead!
-->  <--

Using of __init()!
--> Guestbook <--

123

456

789

Guestbook
up
0
ricasiano at gmail dot com
3 years ago
Objects with __toString() passed within methods with type declarations(either from function arguments or return types) automagically converts it to string.

<?php

class Foo
{
    public function
__toString()
    {
        return
'foo';
    }
}

class
Bar
{
    public function
setAndGetFoo(string $foo)
    {
        return
$foo;
    }

    public function
getFoo(): string
   
{
        return new
Foo();
    }
}

$foo = new Foo();
$bar = new Bar();
// string(3) "foo"
var_dump($bar->setAndGetFoo($foo));
// string(3) "foo"
var_dump($bar->getFoo());
up
0
daniel dot peder at gmail dot com
5 years ago
http://sandbox.onlinephpfunctions.com/code/4d2cc3648aed58c0dad90c7868173a4775e5ba0c

IMHO a bug or need feature change

providing a object as a array index doesn't try to us __toString() method so some volatile object identifier is used to index the array, which is breaking any persistency. Type hinting solves that, but while other than "string" type hinting doesn't work on ob jects, the automatic conversion to string should be very intuitive.

PS: tried to submit bug, but withot patch the bugs are ignored, unfortunately, I don't C coding

<?php

class shop_product_id {
   
    protected
$shop_name;
    protected
$product_id;
   
    function
__construct($shop_name,$product_id){
       
$this->shop_name = $shop_name;
       
$this->product_id = $product_id;
    }

    function
__toString(){
        return
$this->shop_name . ':' . $this->product_id;
    }
}

$shop_name = 'Shop_A';
$product_id = 123;
$demo_id = $shop_name . ':' . $product_id;
$demo_name = 'Some product in shop A';

$all_products = [ $demo_id => $demo_name ];
$pid = new shop_product_id( $shop_name, $product_id );

echo
"with type hinting: ";
echo (
$demo_name === $all_products[(string)$pid]) ? "ok" : "fail";
echo
"\n";

echo
"without type hinting: ";
echo (
$demo_name === $all_products[$pid]) ?  "ok" : "fail";
echo
"\n";
up
0
yanleech at gmail dot com
15 years ago
Maybe we can using unserialize() & __wakeup() instead "new" when creating a new instance of class.

Consider following codes:

class foo
{
    static public $WAKEUP_STR = 'O:3:"foo":0:{}';
    public function foo(){}
    public function bar(){}
}

$foo = unserialize(foo::$WAKEUP_STR);
up
0
martin dot goldinger at netserver dot ch
18 years ago
When you use sessions, its very important to keep the sessiondata small, due to low performance with unserialize. Every class shoud extend from this class. The result will be, that no null Values are written to the sessiondata. It will increase performance.

<?
class BaseObject
{
    function __sleep()
    {
        $vars = (array)$this;
        foreach ($vars as $key => $val)
        {
            if (is_null($val))
            {
                unset($vars[$key]);
            }
        }   
        return array_keys($vars);
    }
};
?>
up
-1
staff at pro-unreal dot de
10 years ago
To avoid instanciating the parent instead of the inherited class for __set_state() as reported by jsnell, you could use late static binding introduced in PHP 5.3:

<?php
class A {
    public static function
__set_state($data) {
        return new static();
    }
}

class
B extends A {
}

$instance = new B();
eval(
'$test = ' . var_export($instance, true) . ';');
var_dump($test);
// -> object(B)#2 (0) {
// }
?>
up
-1
Wesley
12 years ago
Warning __toString can be triggerd more then one time

<?php
if(strstr(substr($obj,0,1024), 'somestuff')
    echo
$obj;
return
'missing somestuff at the start, create container!';

substr() will trigger a __toString aswell as echo $obj;
?>

wich cause a performance issue since it will gather all data twice.

what i used as a hotfix:

<?php
__toString
(){
  if(
null === $this->sToString)
    
$this->sToString = $this->_show();
  return
$this->sToString;
}
?>
up
-2
krisj1010 at gmail.com
19 years ago
If you are attempting to write an abstract/base class which automates the __sleep process in PHP5 you will run into some trouble if the subclasses which are being serialized have private/protected variables you need to be serialized. 

The reason is, even though get_class($this) within the base class will return the subclass -- get_class_vars(get_class($this)) will *not* return the subclass' protected/private variables.  Which makes sense -- using OO principles. 

However, when automating __sleep it becomes necissary to have access to the private/protected subclass variables because their names have to be returned by __sleep.

So here is the work around:
<?php
public function __sleep()
{
...
code ...
$sleepVars    = array_keys((array)$this);
return
$sleepVars;
}
?>

Even though array_keys includes more information about the variable names than just the variable names -- it still seems to work appropriately.
up
-3
osbertv at yahoo dot com
12 years ago
Invoking a class inside a class results in an error.

<?php
class A
{
    public function
__invoke()
    {
        echo
"Invoking A() Class";
    }
}

class
B
{
    public
$a;
   
    public function
__construct()
    {
       
$this->a = new A();
    }
   
    public function
__invoke()
    {
        echo
"Invoking B() Class";
    }
}

$a = new A();
$b = new B();
$a();
$b();
$b->a();

?>

returns
Invoking B() Class
PHP Fatal error:  Call to undefined method B::a()
up
-2
Anonymous
13 years ago
C++-style operator overloading finally makes an appearance with the introduction to __invoke().  Unfortunately, with just '()'.  In that sense, it is no more useful than having a default class method (probably quite useful actually) and not having to type out an entire method name.  Complimenting wbcarts at juno dot com's point class below, the following allows calculating distance between one or more graph points...

<?php

class point {
    public
$x;
    public
$y;

    function
__construct($x=0, $y=0) {
       
$this->x = (int) $x;
       
$this->y = (int) $y;
        }
       
    function
__invoke() {
       
$args = func_get_args();
       
$total_distance = 0;
       
$current_loc = $this;
        foreach (
$args as $arg) {
            if (
is_object($arg) and (get_class($arg) === get_class($this))) {
               
$total_distance += sqrt(pow($arg->x - $current_loc->x, 2) + pow((int) $arg->y - $current_loc->y, 2));
               
$current_loc = $arg;
                }
            else {
               
trigger_error("Arguments must be objects of this class.");
                return;
                }
            }
        return
$total_distance;
        }
   
    }

$p1 = new point(1,1);
$p2 = new point(23,-6);
$p3 = new point(15,20);
echo
$p1($p2,$p3,$p1); // round trip 73.89

?>

Functionally, __invoke() can also be used to mimic the use of variable functions.  Sadly, attempting any calling of __invoke() on a static level will produce a fatal error.
up
-2
Anonymous
13 years ago
Concerning __set() with protected/private/overloaded properties, the behavior might not be so intuitive without knowing some underlying rules.  Consider this test object for the following examples...

<?php
class A {
    protected
$test_int = 2;
    protected
$test_array = array('key' => 'test');
    protected
$test_obj;
   
    function
__construct() {
       
$this->test_obj = new stdClass();
        }
       
    function
__get($prop) {
        return
$this->$prop;
        }
       
    function
__set($prop, $val) {
       
$this->$prop = $val;
        }
    }

$a = new A();

?>

Combined Operators (.=, +=, *=, etc): you must also define a companion __get() method to grant write -and- read access to the property.  Remember, "$x += $y" is shorthand for "$x = $x + $y".  In other words, "__set($x, (__get($x) + $y))".

Properties that are Arrays: attempting to set array values like "$a->test_array[] = 'asdf';" from outside this object will result in an "Indirect modification of overloaded property" notice and the operation completely ignored.  You can't use '[]' for array value assignment in this context (with the exception only if you made __get() return by reference, in which case, it would work fine and bypass the __set() method altogether).  You can work around this doing something like unioning the array instead:

<?php

$a
->test_array[] = 'asdf'; // notice given and ignored unless __get() was declared to return by reference
$a->test_array += array(1 => 'asdf'); // to add a key/value
$a->test_array = array("key" => 'asdf') + $a->test_array; // to overwrite a  key/value.

?>

Properties that are Objects: as long as you have that __get() method, you can freely access and alter that sub object's own properties, bypassing __set() entirely.  Remember, objects are assigned and passed by reference naturally.

<?php

$a
->test_obj->prop = 1; // fine if $a did not have a set method declared.

?>

All above tested in 5.3.2.
up
-2
michal dot kocarek at seznam dot cz
15 years ago
Remember that setters and getters (__set, __get) will work in your class as long as you NOT SET the property with given name.

If you still want to have the public property definition in the class source code (phpDocumentor, editor code completition, or any other reason) when using these magic methods, simply unset() your public properties inside the constructor.
__set/__get function will be called and code reader will see at first sight, which public properties are available.

Example:
<?php
class user {
  
/**
    * @var int Gets and sets the user ID
    */
  
public $UserID;
   private
$_userID;

   public function
__construct() {

     
// All the magic is in single line:
      // We unset public property, so our setters and getters
      // are used and phpDoc and editors with code completition are happy
     
unset($this->UserID);

   }

   public function
__set($key, $value) {
     
// assign value for key UserID to _userID property
  
}

   public function
__get($key) {
     
// return value of _userID for UserID property
  
}
}
?>
up
-2
Anonymous
15 years ago
Serializing objects is problematic with references. This is solved redefining the __sleep() magic method. This is also problematic when parent class has private variables since the parent object is not accessible nor its private variables from within the child object.

I found a solution that seems working for classes that implements this __sleep() method, and for its subclasses. Without more work in subclasses. The inheritance system does the trick.

Recursively __sleep() call parent' __sleep() and return the whole array of variables of the object instance to be serialized.

<?php
class foo {
}

class
a {
  private
$var1;

  function
__construct(foo &$obj = NULL) {
   
$this->var1 = &$obj;
  }

 
/** Return its variables array, if its parent exists and the __sleep method is accessible, call it and push the result into the array and return the whole thing. */
 
public function __sleep() {
   
$a = array_keys(get_object_vars(&$this));
    if (
method_exists(parent, '__sleep')) {
     
$p = parent::__sleep();
     
array_push($a, $p);
    };
    return
$a;
  }
}

class
b extends a {
  function
__construct(foo &$obj = NULL) {
   
parent::__construct($obj);
  }
}

session_start();
$myfoo = &new foo();
$myb = &new b($myfoo);
$myb = unserialize(serialize(&$myb));
?>

This should work, I haven't tested deeper.
up
-2
mastabog at hotmail dot com
18 years ago
In reply to krisj1010 at gmail.com below:

__sleep() handles protected/private properties very well. You should never rely on get_class_vars() to retrieve property names since this function only returns the public properties. Use the Reflection API instead for that purpose. Better yet, if you know which ones you want to save it is always faster to specify the return array manually.
up
-3
Dérico Filho
17 years ago
Since PHP 5.2.0, you'll always get an error like this:
"Object of class foo could not be converted to string"

When one tries to use an object as string, for instance:

class Test{}
echo new Test();

Thus, one way to avoid this problem is to programme the magic method __toString.

However, in the older versions, it would output a string saying that it was an object together a unique obj id. Therefore, the __toString() method must comply with this behaviour.

My suggestion:

class Test{
    function __toString(){
        if(!isset($this->__uniqid))
            $this->__uniqid = md5(uniqid(rand(), true));
        return(get_class($this)."@".$this->__uniqid);
    }

}

echo new Test();

would output something like this:

Test@6006ba04f5569544c10a588b04849cf7
up
-5
danillo dot paiva dot toledo at gmail dot com
10 years ago
While I was studying Ruby I saw as such interesting things as properties created + its getters and setters in just one line.

I tryied to do the same in PHP and this is the code I have

class Father {
    public function __call($name, $args) {
        if(isset($this->$name)) {
            if(isset($args[0]))
                return $this->$name = $args[0];
            return $this->$name;
        }
        return false;
    }
}

class Child extends Father {
    public $country = "Brazil";
    public $state = "Sao Paulo";
}

Sometimes we don't need things like that on all classes but is quite interesting.
up
-4
Travis Swicegood
16 years ago
There is no need to use eval() to mimic mixins (i.e., multiple inheritance) within PHP 5.  You only need to:

<?php

class MyClass
{
    private
$_obj = null;
    public function
__construct($obj)
    {
       
$this->_obj = $obj;
    }

    public function
__call($method, $args)
    {
        if (!
method_exists($this->_obj, $method)) {
            throw new
Exception("unknown method [$method]");
        }

        return
call_user_func_array(
            array(
$this->_obj, $method),
           
$args
       
);
    }
}

?>

You could just as easily add an addMixin() method that would allow you to add multiple objects to an array, and then iterate over that array until you found the right method.  As noted, these are referred to as a Mixins in other languages.
up
-3
rudie-de-hotblocks at osu1 dot php dot net
14 years ago
Note also that the constructor is executed also, and before __set_state(), making this magic function less magic, imho, (except for the ability to assign private members).
up
-4
docey
18 years ago
about __sleep and _wakeup, consider using a method like this:

class core
{

var $sub_core; //ref of subcore
var $_sleep_subcore; // place where serialize version of sub_core will be stored

function core(){
  $this->sub_core = new sub_core();
  return true;
}

function __wakeup()
{
  // on wakeup of core, core unserializes sub_core
  // wich it had stored when it was serialized itself
  $this->sub_core = unserialize($this->_sleep_subcore);
  return true;
}

function __sleep()
{
  // sub_core will be serialized when core is serialized.
  // the serialized subcore will be stored as a string inside core.
   $this->_sleep_subcore = serialize($this->sub_core);
   $return_arr[] = "_sleep_subcore";
   return $return_arr;
}

}

class sub_core
{
var $info;

function sub_core()
{
  $this->info["somedata"] = "somedata overhere"
}

function __wakeup()
{
  return true;
}

function __sleep()
{
  $return_arr[] = "info"
  return $return_arr;
}

}

this way subcore is being serialized by core when core is being serialized. subcore handles its own data and core stores it as a serialize string inside itself. on wakeup core unserializes subcore.

this may have a performance cost, but if you have many objects connected this way this is the best way of serializing them. you only need to serialize the the main object wich will serialize all those below which will serialize all those below them again. in effect causing a sort of chainreaction in wich each object takes care of its own info.

offcoarse you always need to store the eventualy serialized string in a safe place. somebody got experience with this way of __wakeup and __sleep.

works in PHP4&5
up
-9
tom
13 years ago
Note a common pitfall when using __wakeup.

If you unserialize a datastructure, you may not rely on the parent object to have been fully unserialized by the time __wakeup is called. Example

<?php
class A {
public
$b;
public
$name;
}

class
B extends A {
public
$parent;
public function
__wakeup() {
 
var_dump($parent->name);
}
}

$a = new A();
$a->name = "foo";
$a->b = new B();
$a->b->parent = $a;
$s = serialize($a);
$a = unserialize($s);
?>

Expected output: "foo".
Actual output: NULL.

Reason: $b is unserialized before $name. By the time B::__wakeup is called, $a->name does not yet have a value.

So be aware that the order in which your class variables are defined is important! You need to manually order them by dependencies - or write a __sleep function and order them by depencies there. (Currently I can't tell which option I hate more)
To Top