Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of constructor in abstract class in php

I followed this link already before asking - Answer is in JAVA context and this for constructor in PHP .

Since I am starter, my implementation of my PHP code in OOP concepts, so I am really willing to know about the usage and benefits or when to use constructor in PHP abstract class.

Please provide an example in real world context to grab the concept better.

PS - Although I am following PHP Manuals to understand OOP concepts but I am finding it little bit hard to understand, any help with the links/blog to follow is really appreciable.

My Code -

<?php

abstract class grandClass
{
    abstract function grandmethod();

    function __construct()
    {
        echo "I am grandClass constructor";
    }
}

abstract class parentClass extends grandClass
{
    abstract function raiseFlag();

    function grandmethod()
    {
        echo "This is grandmethod!!!","<br />";
    } 

    public function getValue()
    {
        echo "Zero is the blackhole for the numbers!!","<br />";
    }
}

class childClass extends parentClass
{

    function raiseFlag()
    {
        echo "Peaceful thoughts!!","<br />";
    }

}

$myobj = new childClass();
$myobj->raiseFlag();
$myobj->getValue();
$myobj->grandmethod(); 
like image 989
swapnesh Avatar asked Jan 11 '13 06:01

swapnesh


People also ask

Can we use constructor in abstract class in PHP?

Like C++ or Java abstract class in PHP can contain constructor also.

What is the use of constructor in abstract class?

A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor. So if we do not define any constructor inside the abstract class then JVM (Java Virtual Machine) will give a default constructor to the abstract class.

What is the use of constructors in PHP?

A constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Can an abstract class have a constructor?

Like any other classes in Java, abstract classes can have constructors even when they are only called from their concrete subclasses.


1 Answers

Constructor in abstract class is the same as in concrete class. Use constructors when they are needed, for example, if you need to intialize some data or assign some resources.

I'll give you an example:

abstract class Db
{
    protected $pdo;

    public function __construct($pdo)
    {
        $this->pdo = $pdo;
    }

    abstract function select($table, $fields);
}

class Db_Mysql extends Db
{
    public function select($table, $fields)
    {
        // Build MySQL specific select query
        // then execute it with $this->pdo
    }
}

class Db_Pgsql extends Db
{
    public function select($table, $fields)
    {
        // Build PostgreSQL specific select query
        // then execute it with $this->pdo
    }
}

// Usage:
$db = new Db_Mysql($pdo);

$db->select('users', array('id', 'name'));
like image 143
Lauris Avatar answered Oct 04 '22 00:10

Lauris