Home » Php » php – getting Parse error: syntax error, unexpected T_NEW

php – getting Parse error: syntax error, unexpected T_NEW

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have two classes memberdao and member class .I am creating an object of memberdao class inside member class .here is my code

require_once('/../dao/memberdao.class.php');

class Member
{
public $objMemberDao= new MemberDao(); 

}

but it gives an error Parse error: syntax error, unexpected T_NEW in C:\xampp\htdocs\membership\lib\member.class.php on line 9. I am new in php so please help

How to&Answers:

you cannot initialize new objects there.
you must do it in the __construct function;

require_once('/../dao/memberdao.class.php');

class Member
{
  public $objMemberDao; 

  public function __construct()
  {
    $this->objMemberDao= new MemberDao(); 
  }
}

Answer:

create object of MemberDao class into the constructor of Member class

class Member
{
  public $objMemberDao; 

  public function __construct()
  {
      $this->objMemberDao= new MemberDao(); 
  }
}