0

I'm trying to refractor my code by using static methods on models, but I lost the ability to get the Database object.

<?php
$Database = new PDO(.....);
class MyModel{
    public static function getSomething(){
       $test = $Database->somePDOStuff(..);
       ...
    }
}
echo MyModel::getSomething();
?>

That's an a very basic example, but here MyModel doesn't know $Database.

Any idea on how to let them know each other?

2
  • injecting the $databaseto the function seem a good idea. Have you thought about making your database class a static class? There are ways to have it initialize the connection the first time it is called (statically) and reuse it every further calls Commented Jul 11, 2014 at 20:22
  • this has nothing to do wit OOP nor with MVC. Commented Jul 11, 2014 at 22:03

2 Answers 2

1

$Database is out of scope inside your class and function. Consider changing your code to something more like this(below). You send in the PDO object to the function so it has access to it.

<?php
$Database = new PDO(.....);
class MyModel{
    public static function getSomething(PDO $pdo){
       $test = $pdo->somePDOStuff(..);
       ...
    }
}
echo MyModel::getSomething($Database);
?>

Here's another solution. Set a static PDO object inside MyModel. I would not advise using this solution as it's pretty messy. Look at the last solution which is more in the direction i'd go

<?php
    $Database = new PDO(.....);
    class MyModel{
        public static $pdo;
        public static function getSomething(){
           $test = self::$pdo->somePDOStuff(..);
           ...
        }
    }
    MyModel::$pdo = $Database;
    echo MyModel::getSomething();
?>

Consider making your class MyModel include a PDO object that's created in the constructor.

<?php
    class MyModel{
        private $pdo;
        public function __construct() {
            $this->setPdo(new PDO(.....));
        }
        public static function getSomething(){
           $instance = new self();
           $test = $instance->getPdo()->somePDOStuff(..);
           ...
        }
        public function setPdo(PDO $pdo) {
            $this->pdo = $pdo;
        }
        public function getPdo() {
            return $this->pdo;
        }
    }
    echo MyModel::getSomething();
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Use global in your class method

   public static function getSomething(){

       global $Database;
       $test = $Database->somePDOStuff(..);
       //.... 
  }

However, I'd look at avoiding this practice. Why not make $dabase a static variable of a Database class or th MyModel class?

Even better, you could set it with some good old injection by passing it into the funciton like:

  echo MyModel::getSomething($Database);

1 Comment

using a global $Database won't clean up the code. the $Database variable contains a dynamic object and couldn't be static

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.