Wexas
Wexas

Reputation: 1

class constructor not processing when called from inside another class constructor

I have a ready made class X

class X
{
   private $pA;
   function __construct($id=0)
   {
      $pA=new myClass($id);
   }
}

class myClass
{
   private $id;
   function __construct($id=0)
   {
      echo 'constructing....';
   }
}

But there is no echo output the class construction stops at the new operator.

sorry there is () after myclass I mistook

Upvotes: 0

Views: 132

Answers (3)

Nick Rolando
Nick Rolando

Reputation: 26167

Have you actually tried creating an object of type X?

Also, you address class members using $this

$this->pA = new myClass($id);

And the error that @Basti pointed out should be addressed as well.

GL~

Upvotes: 0

summea
summea

Reputation: 7593

It worked for me when I took out the () after myClass() like this:

class myClass
{
    private $id;
    function __construct($id=0)
    {
        echo 'constructing....';
    }
}

Upvotes: 0

Basti
Basti

Reputation: 4042

Parse error: parse error, expecting `'{'' in test.php on line 11

change

class myClass()

to

class myClass

and actually do new X(); and it will work as expected.

you might also want to set error_reporting = E_ALL and display_errors = 1 in your php.ini when developing or debugging code to see whats wrong.

Upvotes: 2

Related Questions