0

I want to load 2 objects from my DB. The first object is the parent and second inherit of the first (in PHP and DB).

I've created 2 class : (it's only a sample not the real code so don't try to correct this ;-) )

class A{
    ...
    public static function get($id){
        $query = "SELECT id,field1,field2 FROM table_A WHERE id = $id";
        $result = request($query);
        return load_object_A_instance($result);
    }
    ...
}

class B extends A{    
    ...
    public static function get($id){
        $query = "SELECT id,field3,field4 FROM table_B WHERE id = $id";
        $result = request($query);
        return load_object_B_instance($result);
    }
    ...    
}

I would instantiate object B with its own properties and with properties of object A in the "same" action. How can I do this ?

I've some ideas but I don't see how to implement them :

class B extends A{    
    ...
    public static function get($id){
        $query = "SELECT id, field3, field4 FROM table_B WHERE id = $id";
        $result = request($query);
        $B = load_object_B_instance($result);

        if($B != empty/null){
            $B = merge(A::get($B->id),$B); // <== that's the part I don't know how to implement
        }
        return $B;
    }
    ...    
}

Edit : I found a first solution (it's not clean but ...)

echo $obj->name;  //show: carlos
echo $obj2->lastname; //show: montalvo here
$obj_merged = (object) array_merge((array) $obj, (array) $obj2);
$obj_merged->name; //show: carlos
$obj_merged->lastname; //show: montalvo here;

Solution found here: How do I merge two objects?

Community
  • 1
  • 1
Airmanbzh
  • 615
  • 1
  • 5
  • 11
  • http://stackoverflow.com/questions/2994758/function-overloading-and-overriding-in-php Does that answer what you want to do? – Paul Davis Mar 30 '12 at 12:54

2 Answers2

0

You can do that with single-table inheritance:http://www.jacopobeschi.com/post/php-laravel-single-table-inheritance

Jacopo Beschi
  • 178
  • 2
  • 6
0

Just because you use classes, does not make it OOP. If you want to merge data from two data source, you retrieve information from them and then a 3rd partt to combined then:

$foo = new A( $connection );
$bar = new B( $connection );

$data = $foo->get( 42 ) + $bar->get( 1 );

Also you should look into DataMapper pattern, and watch some video on the subject of proper OOP:

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Like I said it's only a code sample to show what's my problem. All my "real" code is OO (but your links are interrested). I don't understand this part : "$data = $foo->get( 42 ) + $bar->get( 1 );" How can you add an object to an other ? PS : I don't think it's a good thing to pass your db connection to a constructor when you want to instanciate an object. – Airmanbzh Mar 30 '12 at 13:06
  • @Airmanbzh , if your object will be responsible for directly communication with database, then providing it with connection it connection object, is exactly the right thing to do. Or are you one of the stone age people who still use `mysql_*` as API for DB access ?! – tereško Mar 30 '12 at 13:08
  • my object don't communicate directly with my DB. It's only a representation of my table. – Airmanbzh Mar 30 '12 at 13:12
  • @Airmanbzh , if you object do not query the database, how exactly the get the data to represent the table ? – tereško Mar 30 '12 at 13:23
  • @Airmanbzh , sounds like your code is suffer from [anemic domain model](http://martinfowler.com/bliki/AnemicDomainModel.html) problem. – tereško Apr 01 '12 at 00:56