简体   繁体   中英

PHP5 what's wrong with my syntax?

I've never developed before and I'm a bit puzzled, what is wrong with my syntax here?

private static $instance; //holder of Mongo_Wrapper
public $connected = true; 
private $mongo = null; // The mongo connection affiliation
private $database = null; // The database we are working on

with this function:

        public function mongo_connect($db_name) {
            if (! self::connected) {
                $this->mongo = new Mongo;
                //TODO: error handle this whole sharade: throw new Kohana_Database_Exception('Cant connect', NULL, 503);
                $this->connected = true;
            }

            $this->database = $this->mongo->$db_name; //set the database we are working on

            return $connected;
        }

I'm sorry, wmd-editor is giving me hell posting the code.

Thank you!

edit: $connected isn't static, the problem is it isn't working either with static or with $this. Also, this is a singleton class, I don't know if this is important or not.

edit: this is the rest of the code, here self and this worked properly:

public static function singleton($db_name) {
            if (!isset(self::$instance)) {
                $c = __CLASS__;
                $this->$instance = new $c;
            }
            self::mongo_connect($db_name);
            return self::$instance;
        }
enter code here
if (! self::connected) {

is probably the cause of your error. You only use self when you are trying to access static class members (which connected is not), and you have to use the $-Sign at the beginning, otherwise you are asking for a class constant. So you either have to declare connected as static, or use $this-> to access it.

Take a look at static class members in the PHP manual!

Also you should really try to understand how OOP works, before writing code like this. PHP tells you that you cannot use $this, because you are not in a object context , which means that you never created an object instance using the new .

Maybe the PHP OOP Basics will help you.

Unfortunately, PHP lets you call methods statically which aren't actually, which may be causing the error here. But sooner or later (probably sooner) you will need to understand the OOP basics anyway, so play around with a few simple classes before trying to write code for productive use.

Also take a look at this sample implementation of the singleton pattern .

If you need further help on this issue, please show us how you are calling the connect method!

There we have your problem. Your are doing the following:

self::mongo_connect($db_name);

Which means "call mongo_connect statically on self". What you actually need to do is:

self::$instance->mongo_connect();

Which is equivalent to "call mongo_connect on the singleton instance of self".

But please take a closer look on a basic PHP tutorial, because what you are doing there in your code is mostly wrong...

$this->$instance = new $c;

Is wrong in so many ways... Not only because you are using $this in a static context, but also because you are assigning the created instance to a class member with the name which is * contained in $instance, which seems to be empty... No clue how this can actually work...

self should be used with static members (use $this->connected instead of self::connected ).

UPDATE
private static function mongo_connect($db_name, $instance)
{
if (!$instance->connected) {
....
}
...
return $instance->connected;
}

public static function singleton($db_name) {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        self::mongo_connect($db_name, self::$instance );
        return self::$instance;
    }

x3ro is right. You also need the $this->connected syntax at the end:

return $this->connected;

If you're getting an error message when you use $this->connected, it's because your function isn't a method on the class, but a global function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM