Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP equivalent to C# `: base`

Tags:

c#

php

What would be PHP code equivalent for snippets below:

C# Code :

class Composite : Component
  {
    private List<Component> _children = new List<Component>();

    // Constructor
    public Composite(string name) : base(name)
    {
    }
  }

PHP code?

I am sepcifically looking for : base(name) section. Complete code reference in C# can be found here http://www.dofactory.com/Patterns/PatternComposite.aspx

like image 448
pmoubed Avatar asked Dec 20 '22 21:12

pmoubed


1 Answers

The PHP equivalent is

class Foo extends Bar {
    public function __construct($param) {
        parent::__construct($param);
    }
}

This is explicitly mentioned in the PHP documentation for constructors.

You should keep in mind an important difference between C# and PHP: in PHP, if you don't explicitly call the base constructor it will not be called at all! This is not the same as in C#, where the base constructor is always called (although you can omit the explicit call if a public parameterless constructor exists).

like image 171
Jon Avatar answered Dec 23 '22 09:12

Jon