Jump to content


Photo

Can I call a class from a class?


  • Please log in to reply
1 reply to this topic

#1 HGeneAnthony

HGeneAnthony
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 14 March 2006 - 11:24 AM

I was working with classes in PHP and one thing I generally do is call a class from another class. However, I seem to get the error:

Parse error: parse error, unexpected T_NEW in /www/phpproject/index.php on line 11

I had typed the class:

class Test {
private $user = new User("gene", "password");
}

Is there a way I can make this work? Also, in the User class (due my Java background) I used a constructor with the same name as the class (IE. function User($x, $y)), However, I noticed PHP has the method __construct. However I noticed my way worked. Is it appropriate to use either or, or should I avoid using the same method name as the class in case it's taken out in the future.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 14 March 2006 - 12:49 PM

You'd be best to define User in tests construct.
class Test {

  private $user;

  function __construct() {
    $this->user = new User("gene", "password");
  }

}
Also note that the __construct is only available in php5. If your using an older version you'll need to make a method with the same name as the class as you did above.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users