0

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
4
  • What error message are you getting? ... Commented Jan 9, 2011 at 1:46
  • Fatal error: Using $this when not in object context in Commented Jan 9, 2011 at 1:52
  • Just to make sure, this all is wrapped in some sort of class Foo { }, right? Commented Jan 9, 2011 at 1:57
  • of course, class Mongo_Wrapper. Triple checked it is in scope. Commented Jan 9, 2011 at 1:58

3 Answers 3

2
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...

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

12 Comments

i get: Fatal error: Using $this when not in object context in with $this->$connect and $this->connect
Then you need to make connected a static class member, as is $instance.
this is a singleton class, though, so maybe problem lies there?
No it does not. A well implement singleton pattern should never cause such a problem.
it is implemented from: php.net/manual/en/language.oop5.patterns.php and it worked good so far, added the code above
|
0

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.

1 Comment

I know, only it is indeed scoped properly.
0

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;
    }

5 Comments

okay, I need this as a non static var but $this doesn't work!
It should. But I see that connected initialized to true. Shouldn't it be false?
I've just read your update. From self::mongo_connect I realized that mongo_connect should be a static function which means that there is no $this can be used in this function.
it should be a regular public function, accessed a few times
It's hard to say from what I saw... Maybe you are right. From my perspective, your main problem was in mixing static and instance methods & fields. Anyway, you seem to get an answer.

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.