0

I am trying to access the global variable in a Static Method, But i am getting the Fatal Error:

Fatal error: Call to a member function prepare() on a non-object

Means my static variable can't hold the global variable, but this standard works with the normal method( without static ).

class Payment_Handler {


    private static $dbh;


    function __construct() { 
        global $dbh;

        self::$dbh = $dbh;
    }



    /**
     * Verify Secret Key
     * 
     * @param string $secret_key
     * @return boolean
     */
    static function verify_secret( $secret_key ) {
         // Do the stuff with self::$dbh
         $query = "............";
         $stmt = self::$dbh->prepare($query);
    }
} // End Class

I also tried over google but no luck. Please tell me what mistake i have done, and why it is not accessible by static variable?

1
  • you are mixing the static method with constructor make your function non static and with the object instance call the method something as $obj->verify_secret() so that during object instantiation property is set via __construct Commented Jun 12, 2014 at 11:38

2 Answers 2

2

If you've never instantiated a Payment_Handler object, it would indeed be NULL. I'd write a wrapper function instead of using self::$dbh directly:

class Payment_Handler {
    private static $dbh;
    function __construct() { 
        global $dbh;
        self::$dbh = $dbh;
    }
    static function getDBH(){
       if(is_nul(self::$dbh)) self::$dbh = $GLOBALS['dbh'];
       return self::$dbh;
    }
    static function verify_secret( $secret_key ) {
         $query = "............";
         $stmt = self::getDBH()->prepare($query);
    }
}

... or move away from the static method, and inject the proper requirements in instances.

Sign up to request clarification or add additional context in comments.

2 Comments

Working solution but please note the last sentence in this answer! Also note my answer which elaborates this a bit.
@ToBe yes i got these points during when i searched for solutions. But just want to know the way.
1

Using a global Variable is ALLWAYS a bad idea, using static variables is often a bad idea.

You can give the $dbh object via the constructor to avoid globals:

class Payment_Handler {
    private $dbh;

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

    public function verify_secret( $secret_key ) {
        $query = "............";
        $stmt = $this->dbh->prepare($query);
    }
}

Or even better, use some kind of Dependency Injection:

class Payment_Handler {
    private $dbh;

    public function __construct(){}

    public function setConnector($dbh){
        $this->dbh = $dbh;
    }

    public function verify_secret( $secret_key ) {
        $query = "............";
        $stmt = $this->dbh->prepare($query);
    }
}    

Why is globals bad?

  • Security issue. Your variable (database is even worse) is accessible from anywhere
  • State is uncontrolled. Other parts of code can change your variable without you knowing
  • Testing. It's very difficult to mock something that is global

Why is static bad?

  • Testing. It's very difficult to mock something that is global

Is there a drawback in those "good" samples?

No since PHP uses references internally wherever it can, you wont waste any memory and the performance won't drop by this very few lines of extra code.

(There are other reasons, whole books of them about this)

4 Comments

and how i initiate the Dependency Injection?
In your main code, immediatly after you called the constructor, you call the setConnector method. Allways nice to have a central part of your code where all such classes are initialized and injected propery.
Sorry, I left a static by mistake. These samples are not static. That might explain your question.
hmm, i was confused with the static in your examples. Thanks ;)

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.