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!

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

You got that error because you're attempting to call it publicly. When a method or property is protected it means you can access it from within the child class, not outside of it.

Link to comment
Share on other sites

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.