
Untitled
By: a guest on
Apr 30th, 2012 | syntax:
None | size: 1.37 KB | hits: 8 | expires: Never
Can a class instantiate another class? (PHP)
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;
//}
}
class second {
$fst = new first();
//public showfirsta() {
// $firsta = $this->fst->a;
// echo "Here is first $a: " . $firsta;
//}
}
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();
$fst = new first();
public showfirst() {
$firsta = $this->first->a;
echo "Here is first $a: " . $firsta;
}
class second {
public function showfirst() {
$fst = new first();
$firsta = $fst->a;
echo "Here is first $a: " . $firsta;
}
}
class second {
public $fst;
public function showfirsta() {
$this->fst = new first();
$firsta = $this->fst->a;
echo "Here is first $a: " . $firsta;
}
}