For instance...
if I have a Module class:
class Module{
var $page;
function Module($page){
$this->page = $page;
}
}
and then a specific News module class:
class News extends Module {
function aFunction(){
return $this->page->id;
}
}
can I instantiate News like this:
$page = new Page();
$news = new News($page);
or would I have to do this:
$page = new Page();
$news = new News();
$news->page = $page;
Thank you.
PS: I'm quite new to OOP php, so bear with me lol. I've gone from procedural php to objective-c and now back to objective php. :p Its very difficult trying to compare each to the other.
Yes, they inherit the constructor. This is something you can easily test yourself. For instance:
class Foo {
function __construct() {
echo "foo\n";
}
}
class Bar extends Foo {
}
When you run:
$bar = new Bar(); // echoes foo
However, if you define a custom constructor for your class, it will not call Foo's constructor. Make sure to call parent::__construct() in that case.
class Baz extends Foo {
function __construct() {
echo "baz ";
parent::__construct();
}
}
$baz = new Baz(); // echoes baz foo
All of this is true for your old-style constructors, too (name of class vs __construct).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With