Yasitha
Yasitha

Reputation: 911

get_object_vars explanation needed

one of my php tutorial is showing bellow function. i know it is used to check attribute existence but and i couldn't get its real meaning, i mean what is the meaning of its comment lines ? can anyone explain it to me?

private function has_attribute($attribute) {
            //get_object_vars returns an associative array with all attributes
            //(incl.private ones!) as the keys and their current values as the value
            $object_vars = get_object_vars($this);
            //we don't care about the value, we just want to know if the key exists
            //will return true or false
            return array_key_exists($attribute, $object_vars);
        }

Upvotes: 0

Views: 530

Answers (4)

Berry Langerak
Berry Langerak

Reputation: 18859

Well, the meaning of the comments is explaining what the lines of code do, and they are a rather good description of what they do, too.

<?php

class Foo {
    protected $bar = 'qux';
    public function hasProperty( $name ) {
        return array_key_exists( $name, get_object_vars( $this ) );
    }
}

$foo = new Foo( );
var_dump( $foo->hasProperty( 'does-not-exist' ) ); // false
var_dump( $foo->hasProperty( 'bar' ) ); // true.

Mind you that it's not "good practise" to do stuff like this; normally, properties are private or protected for a reason, and you shouldn't have to determine whether or not a certain property exists.

Upvotes: 0

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Check out this example :

<?php

class someClass
{
    public $someVar1;
    public $someVar2;
}

$cl = new someClass();

$vars = get_object_vars($cl);

print_r($vars);

?>

Output :

Array
(
    [someVar1] => 
    [someVar2] => 
)

What your code does is check whether your object instance has a specific attribute.

E.g.

$cl->has_attribute("someVar1");

would return true.


References :

Upvotes: 4

Fluffeh
Fluffeh

Reputation: 33512

This function sits within a class and returns an array of all the attributes (variables within the class) and returns you an array with the keys set to the attribute names.

it includes private attributes (which means that normally they cannot be accessed anywhere except by the object itself.

It doesn't check the attribute value at all and finally returns an array with the $attribute that was passed to it with a true or false telling whether it exists or not.

Upvotes: 0

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

Here's a definition of get_object_vars()

array get_object_vars ( object $object )

Gets the accessible non-static properties of the given object according to scope.

In this context it means thaat the function will check if the $attribute has been assigned to the class you run it in, and return true or false.

i.e.

class Foo {
   public $bar = "e";

   private function has_attribute($attribute) {
            $object_vars = get_object_vars($this);
            return array_key_exists($attribute, $object_vars);
   }
}

$foo = new Foo;
$foo->has_attribute('bar'); //true
$foo->has_attribute('fooBar'); //false

Upvotes: 0

Related Questions