Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How implement composition in php

I want to implement next fragment of diagram, using PHP.

See composition example diagram below:

enter image description here

We can implement composition in Java using inner classes.

But there is no analog of "inner class" in PHP. Of course, there are traits. But we can use it in more than one class.

I've implemented composition like this:

class Head {
    private static $instance = NULL;

    private function __construct(){}

    public static function getInstance() {
        $traces = debug_backtrace();
        if (strcmp($traces[1]['class'], 'Human')) {
            echo "<br>Only human has head"; // Exception can be thrown here
            return NULL;
        }
        if (!static::$instance) static::$instance = new self();
        return static::$instance;
    }

    public function __toString() {
        return 'Head';
    }
}

class Human {
    private $head;

    public function __construct() {
        $this->head = Head::getInstance();
    }

    public function __toString() {
        return 'I have ' . $this->head;
    }
}

class Plant {
    private $head;

    public function __construct() {
        $this->head = Head::getInstance();
    }
}

$human = new Human();
echo $human;

$superman = new Plant();

Is it right to do so?

Is there better way to implement composition relationship in PHP?

like image 707
profport Avatar asked Sep 01 '16 16:09

profport


People also ask

What is a composition in PHP?

Composition is another type of relationship between classes which allows one class to contain another. Where inheritance can be thought of as a Is-A relationship, composition can be thought of as a Has-A relationship. For example, a car has an engine. Just like inheritance, composition allows for code reuse.

How Oops is implemented in PHP?

Object Oriented ConceptsYou can think of a class as a template for making many instances of the same kind (or class) of object. Object − An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

How does composition work programming?

Composition is one of the fundamental concepts in object-oriented programming. It describes a class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects. You can find such relationships quite regularly in the real world.

Why is composition over inheritance?

To favor composition over inheritance is a design principle that gives the design higher flexibility. It is more natural to build business-domain classes out of various components than trying to find commonality between them and creating a family tree.


1 Answers

It looks like you are really confused about what "composition" is in OOP.

Composition is an association between two classes, where , for an instance of first class to be instantiated, it is mandatory to provide it with second class. In you particular diagram for Human to exists, it requires a Head. In code it would be:

class Head {
}

class Human {
    private $head;
    public function __construct(Head $head) {
       $this->head = $head;
    }
}

$bob = new Human(new Head);

And that's it. No debug_backtrace() and no singleton anti-pattern required. And, btw, this would look almost exactly the same in Java too, even with the option to have inner classes.

As for your posted code. This would be wrong:

if (strcmp($traces[1]['class'], 'Human')) {
    echo "<br>Only human has head"; // Exception can be thrown here
    return NULL;
}

The diagram did not say that only humans have a head. Only that human must have a head to be instantiated. You can also have $skipper = new Dog(new Head);, which would be perfectly fine.

like image 92
tereško Avatar answered Sep 19 '22 18:09

tereško