What is the advantage of overloading objects in PHP?

PHP object overloading is explained here .

This basically allows you to define some user actions when accessing an inaccessible object or method.

What is the practical use for this feature?

+5
source share
8 answers

Usually these methods are useful when you are communicating with a third-party API or when the structure of the method / members is unclear.

Say you are writing a general XML-RPC wrapper. Since you do not know the methods available to you before downloading the WDL file, it makes sense to use Overloading.

Then instead of writing the following:

$xmlrpc->call_method('DoSomething', array($arg1, $arg2));

You can use:

$xmlrpc->DoSomething($arg1, $arg2);

which is more natural syntax.


, .

, : . - , :

class ShortcutDemo {
  function &__get($name) {
    // Usually you want to make sure the method
    // exists using method_exists, but for sake
    // of simplicity  of this demo, I will omit
    // that logic.
    return call_user_method('get'.$name, $this);
  }

  function __set($name, &$value) {
    return call_user_method('set'.$name, $this, $value);
  }

  private $_Name;

  function &getName() { return $this->_Name; }
  function setName(&$value) { $this->_Name = $value; }
}

, - :

$shortcut->Name = 'Hello';
+4

, ( ), , . , getter :

$obj->setName("Chacha");
$obj->setRep(10000000000000000000000);

$obj->Name = "chacha";
$obj->Rep = 100000000000000000;

.

- , , , , . Magic Methods, , , .

. , , .

Magic Method , , , , 't .

, Magic Methods, , , .

0

, isset unset. , , $a, , , .

, , , /member.

++

0

, (, , ).

<?php

// Class A is final, so we can't make subclasses.
final class A
{
  public function hello( $callback )
  {
    echo call_user_func( $callback, 'hello world' );
  }
}

// so instead, we make a wrapper class that will take an instance
// of A as an aggregate
class B
{
  private $a;

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

  // this mimics inheritance on the aggregate object
  // method calls are automatically forwarded to instance of A
  // if they are valid
  public function __call( $method, $args )
  {
    if ( method_exists( $this->a, $method ) )
    {
      return call_user_func_array( array( $this->a, $method ), $args );
    }
    throw new Exception( "Method [$method] not found." );
  }
}

class C extends B
{
  // This mimics overriding an "inherited" method
  public function hello( $callback )
  {
    echo call_user_func( $callback, 'bonjour le monde' );
  }
}

$a = new A;

$b = new B( $a );
$c = new C( $a );

$b->hello( 'strtoupper' );
$c->hello( 'strtoupper' );
0

, - - , : , , . , , ( ) . , " -, , ++ , ". .

, , , , . "" .

: Groovy Builders . , , .

0

, , , Linq, Object Relational Management (ORM). ( ), .

.

include('blibrary/bLinq.class.inc');

$linq = new bLinq(new bLinqSql('mysql://dsn'));
$r = $linq->from('Users')
          ->password
          ->select();

SQL:

SELECT `password` from Users;

password select .

:

(array)$r->password;   // which outputs an array multiple results of password;
(string)$r->password;  // which outputs a string of the first password hash;
$r->password[2];       // which outputs a string of the third password hash;

, "password" " " .

0

__get __set , .

$user = new User();
echo $user->Profile->views;

() SQL users.id = profile.user_id.

0

Properties (e.g. in Python or C #). For example, if you use something like in Nette , you create some class that shows some property as public:

<?php
class Foo extends Object
{
    public $bar;
}

You can access this property in a natural way $instance->bar. But when you want to do some validation, etc., you just add getter and setter:

<?php
class Foo extends Object
{
    private $bar;

    public function getBar()
    {
        return $this->bar;
    }

    public function setBar($bar)
    {
        if ($bar === NULL) throw new Exception('…');
        $this->bar = $bar;
    }
}

And yet you are using $instance->bar. But when you do $instance->bar = NULL;, he likes to call $instance->setBar(NULL);.

0
source

All Articles