Prashanth Shyamprasad
Prashanth Shyamprasad

Reputation: 837

Is there any option for dynamical extension of class in PHP?

What I meant is to change the base class for a derived class dynamically. For example there are two classes B1 and B2. When I run a php script with parameter class=b1, a class (say class D) defined in that script should extend class B1 else if parameter is class=b2 it should extend B2.

Expect something like this

class B1{
    // some codes 
}

class B2{
    // some codes 
}

switch($_GET['class']){
   case 'b1': $baseclass = "B1"; break;
   case 'b2': $baseclass = "B2"; break;
}


class D extends $baseclass{
    // some codes 
}

Is there any option in PHP like that? Please suggest me if any option is available.

Thanks in advance.

Upvotes: 3

Views: 74

Answers (3)

user2054011
user2054011

Reputation:

It is never possible in php language.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158250

I would not suggest to dynamically extends classes. Although it is possible it is a really bad design that will have many unwanted side effects. To tell at least one: How will you generate an API documentation for that class(es)? (Knowing this is the smallest problem)

With the additional info from the comments: you want to build something like a ORM that can work with different databases, I was thinking first about PDO. It allows to access a large set of databases using the same interface.

If you for whatever reason need a non PDO solution you could make a design like this:

<?php

class DatabaseStorage {

    /**
     * @var DB_Mysql|DB_SQLite|DB_Postgres ...
     */
    protected $database;


    /**
     * @param DB_Mysql|DB_SQLite|DB_Postgres ... The inner database to be used
     */
    public function __construct($database) {
        $this->database = $database;
    }

    /**
     *  Connect to the database
     */
    public function connect($host, $user, $password) {
        $this->database->connect($host, $user, $password);
    }


    /**
     * Query the database
     */
    public function query($query) {
        return $this->database->query($query);
    }

    // other methods may follow
}

Then use the class like this:

$databaseType =$_GET['databaseType'];

// you should check the value before using it!!
/*if(!is_valid_database_type($databaseType)) {
    die('BAD input');
}*/

$databaseClass = 'Database' . $databaseType;
$database = new $databaseClass();

// use $database as argument to the storage constructor
$storage = new DatabaseStorage($database);

However this is just an example. You'll find other ways to prevent from dynamic code generation,

Upvotes: 1

Prashanth Shyamprasad
Prashanth Shyamprasad

Reputation: 837

I have found a solution for my problem. Guys please verify whether this is a correct approach or not.

Now i store B1 and B2 in two seperate files but class name is same and require the file needed dynamically.

b1.php
class B{
    // some codes 
}

b2.php
class B{
    // some codes 
}

now extend D like this

$baseclass = $_GET['class'];

require_once($baseclass.'.php');

class D extends B{
    // some codes 
}

this one works for me. :)

Upvotes: 1

Related Questions