Overloading in php

Can any one tell what does below given lines means?

The lines below are copied from PHP manual:


It is not possible to use overloaded properties in other language constructs than isset(). This means if empty() is called on an overloaded property, the overloaded method is not called.

To workaround that limitation, the overloaded property must be copied into a local variable in the scope and then be handed to empty().

BUT this is not true that we cant call empty() on overloaded properties, when i called empty() , it triggered __isset()


It is a documentation bug:

class PropNameReturn {
    function __isset($propname){
          return true;

    function __get($propname){
          echo 'Yes, it IS called!'.PHP_EOL;
          return $propname;
$o = new PropNameReturn();
//Yes, it IS called!
$b = new stdClass();

Looks like the manual is wrong. This also works in PHP 5.2

By the way it seems that __get() is called when used by empty(), but the result also depends on __isset().

empty() returns true only if __isset() returns true and __get() returns an empty value.

See this example code:

class Foo {
    function __get($name) {
        if ($name == "bar")
            return '';
    function __isset($name) {
        if ($name == "bar")
            return true;
$foo = new Foo();
echo $foo->bar . PHP_EOL;  // outputs "" - correct
echo var_export(isset($foo->bar)) . PHP_EOL; // outputs "true" - correct

$bar = $foo->bar;

// outputs both "true" -- so the manual is wrong here
echo var_export(empty($foo->bar)) . PHP_EOL;
echo var_export(empty($bar)) . PHP_EOL;

Also see this bug report

Apparently I didn't notice the link you included in your own question, so here's a basic breakdown of overloading:

Overloading in PHP is the concept of dynamically creating methods and parameters. Consider a basic scenario:

class MyClass {
  public $foo = 'bar';

$Instance = new MyClass();
echo $Instance->foo;

We would all expect that to print "bar," and it would. Here is that same example using overloding:

class MyOverloadedClass {
  public function __get($variable) {
    return $variable == 'foo' ? 'bar' : '';

 $Instance = new MyOverloadedClass();
 echo $Instance->foo;

In this second example, because MyOverloadedClass::$foo does not exist, the __ge method will be executed, and will be passed the variable that the user asked for (in this case, "foo"). My code inside of the __get method would determine that the user is looking for foo, and return "bar"

Language constructs are parts of the PHP language. In other words, the parser interprets them and knows what to do, without having to lookup function references. Some examples are exit, die, print, echo, isset and empty.

isset() is the only language construct to work with overloading. The manual page describes an overloaded method __isset which you can add to your class, and will be executed every time someone calls isset on a property of that class. Calling empty() on an overloaded property, however, will always return true and likely throw an E_NOTICE. Here's why:

Disclaimer: calling empty() on an overloaded property DOES seem to execute __get(), IF AND ONLY IF you have an __isset() method that returns true for the passed variable name:

empty() will not call your overloaded __get() method. It will look in the class definition to see if that parameter exists. If it does, it'll evaluate whether or not it is empty. However, accessing undefined class parameters throws an error. In my first example, I would have thrown an error by asking for $Instance->bar, because it doesn't exist. This is the same error you would get if you asked for

empty( $Instance->foo );

In the second example. I hope this helps.

See the page you mentioned for additional details on how to apply this to functions, or setting variables.

Need Your Help

Advanced List usage (filter enumerable items)

c# .net silverlight windows-phone-7 listbox

I have a ListBox in my App. Also I have a collection of the items and each of this items have the collection of the items inside. So, actually it's a tree and I want to display them like folders/fi...

sql syntax in java with string

java mysql sql

I have this query( the parameters of the query are given by input: both table and name and password are string given by text field.