开发者

Do classes which extend other classes inherit the same constructor?

开发者 https://www.devze.com 2023-02-16 08:29 出处:网络
For instance... if I have a Module class: class Module{ var $page; function Module($page){ $this->page = $page;

For instance...

if I have a Module class:

class Module{

  var $page;

  function Module($page){
    $this->page = $page;
  }
}

and then a specific Ne开发者_如何转开发ws 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).


One solution would be to specify a constructor for News, function News($page) {...} which would call the parent constructor like this: parent::Module($page). You're trying to encapsulate $page, and accessing it directly defeats this purpose.


I'm not familiar with PHP, but the last piece of code would look very bizarre in any OOP language I know. In OOP languages the constructor of News would implicitly or explicitly call the constructor of Module.


First off, you should use __construct() instead of the name of the class as the constructor as this is more modular.

As with any other OOP language, the constructor of a child class is inherited by its parent, so your first example will indeed work.

One thing to keep in mind, though, is that a parent constructor is not implicitly called by the parent. So if you define a constructor in News, the first example won't work anymore unless you have the News constructor handle $page on its own, or call parent::__construct($page);

Another issue is that the encapsulation the classes provide is violated by $page being public. The var keyword is a remnant of PHP 4, and you should declare $page as private. It should be visible to and handled by only the Module class. For instance, why does News need aFunction?


you can inherit the attributs of another class for example the italienChef inherited the makeChiken from the class Chef so when we run it it gives us // the chef makes chiken

class Chef{

    function makeChiken(){
        echo "the chef makes chiken <br>";
    }
}

class ItalienChef extends Chef{
   function makePasta(){
    echo "the chef makes pasta";
   }
   
$italienchef = new ItalienChef();
$italienchef-> makeChiken();
  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号