Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

In PHP, I have a parent/child class and am using dependency container to load them. I am trying to use the same database connection on all of them without duplicating the object. CLARIFICATION: MY CODE WORKS, I JUST DON'T WANT TO DUPLICATE THE DATABASE OBJECT.

SIDENOTE: Also, someone made a comment that this code is not OOP. Why? And how can I make it OOP?

Can someone who understands this stuff better than me please have a look and let me know if I'm doing it right and if not, what I need to change in my code?

Thanks

Brian

UPDATE: Ok, well I used $this->_db to pull data from the database in a child class method (and my class stores that in the property $this->_db->result) ...
So then I went into the parent class and print_r($this->_db->result) and it worked... So that leads me to believe that I am passing the database object and not duplicating it (maybe?).... However, would still be nice if someone could tell me why my code would not be considered OOP? As someone had commented that it was not OOP. Thanks

<?php

    include('mysqliClass.php');
    $db = new mysqliObject();

    depCon::$_database = $db;
    $myObject = depCon::createNewObject();

    class depCon {

      public static $_database;

      public static function createNewObject() {

        $newObject = new myChild();
        $newObject->setDatabase(self::$_database);

        return $newObject;
      }
    }

    class myParent {

      private $_db;

       public function __construct() {
        }

      public function setDatabase($databaseConnection) {
        $this->_db = $databaseConnection;
      }

    }

    class myChild extends myParent {

        private $_db;

        public function __construct() {
            parent::__construct();
        }

        public function setDatabase($databaseConnection) {
            $this->_db = $databaseConnection;
            parent::setDatabase($this->_db);
        }

    }

    ?>
share|improve this question
2  
@All: If you don't have anything constructive to say, go away. If you have advice for improving this code, post an answer. If there's a valid reason to close this, vote. If you like waffles, honk. –  Shog9 Feb 8 '13 at 22:05
    
Why have you overloaded setDatabase in myChild? –  Winston Ewert Feb 8 '13 at 22:30
    
What am I doing to overload it? I am just attempting to get the parent class to use the same database connection. If I don't do that, it won't let me do $this->_db in parent class methods. –  Brian Patterson Feb 8 '13 at 22:39
    
OK sorry, edited code, didn't put the property declaration in child class. Typo. –  Brian Patterson Feb 8 '13 at 22:45
    
To be on-topic here, you really should be showing actual code, not what appears to be some mocked up code. We prefer questions that are general code reviews, not solve this one problem for me, as this one is. I won't do anything with this question, but just so you know in future. –  Winston Ewert Feb 9 '13 at 16:26
show 3 more comments

2 Answers 2

up vote 1 down vote accepted

Don't use a private attribute for the database, use a protected one. That way both classes can share the same $_db, and you only need one setDatabase.

As for your code being OO or not, there simply isn't enough code here to make the claim one way or the other.

share|improve this answer
add comment

Why should an object know about a database (in an OOP sense)? I'm definitely no friend of the Singleton pattern, but a database is in most projects a valid use case.


Your code might not be OO but Service Oriented as you are injecting the database(-service) to your objects.

share|improve this answer
1  
This looks to be dependency-injection which is certainly OO, so I've got not clue why you think its Service Oriented. –  Winston Ewert Feb 9 '13 at 16:27
    
I think this is a question of the definition. From my point of view using objects itself is not OO. In a plain OO approach the Object shouldn't know anything about the persistence layer. It's just holding some data and can to some operation. (Some kind of factory knows about the persistence details.) DI is usually related to some kind of Services, "nobody" is injecting plain objects. –  mnhg Feb 10 '13 at 12:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.