Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.