Difference between these two OOP scenarios?

Stephan-v

I am confused what the difference or the performance gain would be between these 2 scenario's. Why would one be chosen over the other?

Parent class:

class exampleB
{
    public function __construct($arg1, $arg2)
    {
        // Do something with the arguments.
    }
}

Child class A

class exampleA extends exampleB
{
    public function make($arg1, $arg2)
    {
        parent::__construct($arg1, $arg2);
    }
}

Running the first example:

$exampleA = new exampleA();
$exampleA->make('arg1', 'arg2');

The second example would be:

Child class A

class exampleA extends exampleB
{
    public static function make($arg1, $arg2)
    {
        return new static($arg1, $arg2);
    }
}

Running the second example:

exampleA::make('arg1', 'arg2');

Could somebody tell me the advantages and/or disadvantages between these 2 scenarios? The reason I have these example because I do not want to override the constructor of my parent class.

PeeHaa

You should do neither and use the constructor to initialize the object. The object must be in a valid state after construction.

The reason I have these example because I do not want to override the constructor of my parent class.

Then simply just don't define the constructor in the child class.

class Parent {
    public function __construct() {
        echo 'parent ctor called';
    }
}

class Child extends Parent {}

new Child(); // echo's parent ctor called

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OOP relationships between two classes

From Dev

Difference between these 2 scenarios in load testing

From Dev

Difference between Jmeter load test scenarios

From Dev

What is the difference between `this` and `prototype`? javascript oop

From Dev

Difference between OOP basics vs SOLID?

From Dev

Difference between the two quaternions

From Dev

Is there a difference between the two?

From Dev

Difference between these two DefaultListCellRenderer?

From Dev

Difference between these two codes?

From Dev

Difference between these two DefaultListCellRenderer?

From Dev

Difference between two tables

From Dev

Difference between two dates #

From Dev

DIfference Between two Clusters

From Dev

Is there a difference between these two operations?

From Dev

What is a good example that shows the difference between OOP and procedural programming in JavaScript?

From Dev

What is a good example that shows the difference between OOP and procedural programming in JavaScript?

From Dev

Sql: difference between two dates

From Dev

Difference between these two async implementations

From Dev

Difference between two methods of malloc

From Dev

Difference between two soap requests

From Dev

Is there any difference between these two loops?

From Dev

Difference between two pointer variables

From Dev

Percentage difference between two events

From Java

Get difference between two lists

From Dev

SQLite difference between two sets

From Dev

Difference between these two function formats

From Dev

Difference between two regex: "([^"]*)" vs "(.*?)"

From Java

Getting the difference between two sets

From Dev

Difference between two dates in postgresql

Related Related

HotTag

Archive