0

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

3
  • And why do you believe that your guess is correct? Commented Feb 23, 2012 at 1:57
  • Did you add <?php and ?> on your files ? Your code works like a charme :-) Commented Feb 23, 2012 at 2:20
  • I did, I don't know why it doesn't work. I am using php53 Commented Feb 23, 2012 at 2:23

3 Answers 3

2
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.

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

Comments

0

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

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

1 Comment

And like @Shredder said, it's a good idea to use $this-> when referring to a class member variable.
0

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~

2 Comments

Did you try creating an X object? $myX = new X();
@Wexas I copy+pasted the code you have above, and added $myX = new X(); after it (all within <?php ... ?> tags of course) and it works fine.

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.