Jump to content

Inheritance Question - Quick and Easy I promise!


sfc

Recommended Posts

If I have a class DatabaseObject that has the follow method:

protected function attribute () {
            
		return get_object_vars($this);
            
        }

 

And I call this method from the class user which extends the DatabaseObject class (i.e. $user->attribute), what will "$this" refer to?  The parent or the child?

 

Thanks!

 

$this refers to whatever instance you are operating on:

 

$db = new DatabaseObject;
$var1 = $db->attribute(); // $this refers to the $db instance
$db = new UserObject;
$var2 = $user->attribute(); // $this refers to the $user instance

And of course there's no point in using get_object_vars on a class

 

Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property have not been assigned a value, it will be returned with a NULL  value.

So I am a little confused.

 

The user class extends databaseobject so I should be able to call a protected class from the instantiated child right?

 

i.e. $user->save();

 

Where save is protected method in the databaseobject class.

 

But when I just did I got an error saying that I called a protected method.  As soon as I change the save() method to public it worked perfectly.

 

Can someone explain?  Do you need more information?

 

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.