Khurram Ijaz
Khurram Ijaz

Reputation: 1864

scope resolution operator doesn't give compile time error in php

i created a small PDO class and spent hours in debugging it and could not find a small typo which was causing every thing to fail. To demonstrate below is the buggy code.

    class MyPDO  extends PDO
{
    private static $instance = null;
    function  __construct(){
        try{
        parent::__construct("mysql:host=localhost;port=3306;dbname=blog", "root", "");
        parent::setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        }catch(PDOException $e){
            echo 'Exception in constructor'.print_r($e->trace(),true);
        }

    }

   static public function getDB(){
        if(self::$instance == null){
            self:$instance = new MyPDO();
        }
        return self::$instance;
    }

    function selectAll($sql){
            $stmt = self::$instance->prepare($sql);
            $stmt->execute(array(":cat_id"=>1));
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

}

What i want to confirm that if any one has already seen it or is it a bug or it could be used for some thing else i have no knowledge of.

There is a problem with the following line i wrote.

self:$instance = new MyPDO();

it should be the scope resolution operator with double colon i.e.

self::$instance = new MyPDO();

To my surprise no warning or error is generated with a single colon .. If any one knows about this please share.

Upvotes: 1

Views: 126

Answers (1)

mario
mario

Reputation: 145512

In this instance you are defining a jump label (think goto).

self:  $instance = new MyPDO();

And $instance will just become a local variable.

So yes, it is sort of a bug that you can use a reserved word for a label.

Upvotes: 4

Related Questions