3

I tried this and I get an error when I try to instantiate class "first" inside of class "second".

The commented sections inside of class "second" cause errors.

class first {
    public $a;

    function __construct() {
        $this->a = 'a';
    }
}

class second {
    //$fst = new first();
    //public showfirst() {
        //$firsta = $this->first->a;
    //  echo "Here is first \$a: " . $firsta;
    //}
}

EDIT:

This results in a server error even though all I have in class "second" is the instantiation of class "first".

class second {
    $fst = new first();
    //public showfirsta() {
    //  $firsta = $this->fst->a;
    //  echo "Here is first \$a: " . $firsta;
    //}
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
  • That `$fst` seems to have been conjured out of nowhere. – BoltClock Feb 13 '12 at 06:24
  • Yeah, so? Can't I create a $fst that is a new instance of class first? That's what I want to do. – Buttle Butkus Feb 13 '12 at 06:25
  • No problem in instantiatin another class, you just need to refer to it with the right variable though...`$firsta = $fst->a` for ex. The object of first() was assigned to $fst, not $this->first – Damien Pirsy Feb 13 '12 at 06:26
  • Thanks, that was my mistake, but the fundamental error is at class instantiation. See my edit. $fst = new first(); does not work at all. – Buttle Butkus Feb 13 '12 at 06:31
  • 1
    Because you can't create an object while declaring a class variable; see my answer – Damien Pirsy Feb 13 '12 at 06:33
  • possible duplicate of [PHP property as object](http://stackoverflow.com/questions/5984360/php-property-as-object) – Gordon Feb 13 '12 at 07:54

3 Answers3

11

try this:

class First {
    public $a;

    public function __construct() {
        $this->a = 'a';
    }

    public function getA() {
      return $this->a;
    }
}

    class Second {
        protected $fst;
        public function __construct() {
          $this->fst = new First();
        }

        public function showfirst() {
           $firsta = $this->fst->getA();
           echo "Here is first {$firsta}";
        }
    }

    $test = new Second();
    $test->showfirst();
busypeoples
  • 737
  • 3
  • 6
  • That works very well! But why do you put $firsta in {} inside the echo statement? – Buttle Butkus Feb 13 '12 at 06:45
  • you can use them to interpolate variable expressions. but in this case, this will also work without ofcourse.http://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php – busypeoples Feb 13 '12 at 06:49
7
$fst = new first();

You cannot declare a variable that instantiates a new class outside of a function. The default value cannot be variable. It has to be a string, a number, or possibly an array. Objects are forbidden.

public showfirst() {

You forgot the word function in there.

    $firsta = $this->first->a;

You have no class variable $first declared. You named it $fst and would reference it as $this->fst.

    echo "Here is first \$a: " . $firsta;
}

For your purposes (whatever those may be):

class second {
    public function showfirst() {
        $fst = new first();
        $firsta = $fst->a;
        echo "Here is first \$a: " . $firsta;
    }
}
animuson
  • 53,861
  • 28
  • 137
  • 147
  • 1
    In your example, the $fst instance would die as soon as the function finished executing, right? If I need $fst for other operations in the class, what would I do? And yes you are right about my mistakes with the "function" keyword missing and other stuff. – Buttle Butkus Feb 13 '12 at 06:43
4

You can instantiate a class inside another. In your case, in your both example you keep referring to the wrong variable. Also, you can't assign a class in the declaration of a property:

class second {

    public $fst;

    public function showfirsta() {
    $this->fst = new first();
    $firsta = $this->fst->a;
    echo "Here is first \$a: " . $firsta;
    }
}
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • It seems strange that you can't initialize the variable immediately to an instance of another class. But that does solve the problem. Also, this class instance will die with the calling object, right? – Buttle Butkus Feb 13 '12 at 06:38
  • What do you mean? Anyway, you can always return the instance: `return $this->fst`. If you put the instantiation in the constructor, also, you'll have it assigned as soon as the class is instanciated; than you'll have the $this->fst available to the whole class – Damien Pirsy Feb 13 '12 at 06:47