Posted by Tim Van Wassenhove on 12/31/06 12:12
> class A
> {
> public function methodA(){
> //some useful code
> }
> class B
> {
> private A $instanceOfA; //but I am really not sure if this one's works
>
> public __construct() {
> $instanceOfA=new A();
> }
> public function methodB(){
> $this->instanceOfA->methodA();
> }
> }
> }
> Anyway this is legal in terms of OO-Programming and also a well used
> method for composing classes. I tried this out in PHP but it doesn't
> seem to work. Obviously you'll have to create the instance within the
> function/method you want to use it. Like:
What you really wanted to do in the constructor of B was the following:
public __construct() {
$this->instanceOfA = new A();
}
Because with $instanceOfA you create a local (in that function) variable...
--
Tim Van Wassenhove <url:http://www.timvw.be/>
Navigation:
[Reply to this message]
|