0

I have a class User. Then I extend User with some extra information in Profile.

My constructor for Profile looks like this:

function __construct($user_id) {
  // This function sets all of User's attributes
  $this->set_user_data($user_id); 

  // This function sets the extra attribute for Profile.
  $this->set_follow_status(); 
}

The second function uses $this->user_id, an attribute set in the first function. I get this error from the second function: Fatal error: Using $this when not in object context.

Am I forgetting something about extending objects in PHP?

1 Answer 1

3

Is the set_follow_status method declared as static? Static methods are executed in the scope of the class, not in the scope of the object, and therefore have no access to any specific instance ($this is not set inside of the method).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is. Good catch and thanks :) Too fast for me to accept as answer so hang on a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.