0

I implemented a class like this:

class ClassRoom
{
  public $desired_connection;  // variable of type DesiredConnection

  //Constructor
  public function __contruct()
  {
    $this->desired_connection = new DesiredConnection(); //Initialize
  }

  public function save(){   
    //Save Desired Connection
    $this->desired_connection->saveDesiredConnection();  
  }
}

Upon calling $classroom->save(), runtime does not recognize $this->desired_connection as DesiredConnection object and throws this error:

"Call to a member function save() on a non-object."

When I output, gettype($classroom->desired_connection), it outputs NULL.

Any suggestions on what I may be doing wrong here ?

0

1 Answer 1

1

Make sure your __construct function is named appropriately:

public function __construct() {
  // ...
}

PHP will only recognize magic functions that match the names it's looking for!

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

2 Comments

Thanks! Can't believe that this was a typo!
Happens to all of us :^) Cheers!

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.