简体   繁体   English

PHP5我的语法有什么问题?

[英]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. 对不起,wmd-editor正在给我发布代码。

Thank you! 谢谢!

edit: $connected isn't static, the problem is it isn't working either with static or with $this. 编辑: $ connected不是静态的,问题是它无法使用static或$ 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: 编辑:这是代码的其余部分,这是self,并且可以正常工作:

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. 仅在尝试访问静态类成员(未连接的静态成员)时才使用self,并且必须在开始时使用$符号,否则将要求一个类常量。 So you either have to declare connected as static, or use $this-> to access it. 所以你要么必须声明连接为静态,要么使用$ this->来访问它。

Take a look at static class members in the PHP manual! 在PHP手册中查看静态类成员

Also you should really try to understand how OOP works, before writing code like this. 在编写这样的代码之前,你应该真正尝试理解OOP的工作原理。 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 . PHP告诉您不能使用$ this,因为您不在对象上下文中 ,这意味着您从未使用new创建对象实例。

Maybe the PHP OOP Basics will help you. 也许PHP OOP Basics会帮助你。

Unfortunately, PHP lets you call methods statically which aren't actually, which may be causing the error here. 不幸的是,PHP允许你静态调用实际上没有的方法,这可能导致错误。 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. 但是迟早(可能更快),您仍然需要了解OOP基础知识,因此在尝试编写可用于生产性用途的代码之前,请尝试一些简单的类。

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! 如果您在此问题上需要进一步的帮助,请告诉我们您如何调用connect方法!

There we have your problem. 那里我们有您的问题。 Your are doing the following: 你正在做以下事情:

self::mongo_connect($db_name);

Which means "call mongo_connect statically on self". 这意味着“静态调用mongo_connect”。 What you actually need to do is: 你真正需要做的是:

self::$instance->mongo_connect();

Which is equivalent to "call mongo_connect on the singleton instance of self". 这相当于“在自我的单例实例上调用mongo_connect”。

But please take a closer look on a basic PHP tutorial, because what you are doing there in your code is mostly wrong... 但请仔细看看基本的PHP教程,因为你在代码中所做的事情大多是错误的......

$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... 在很多方面都是错误的……不仅是因为您在静态上下文中使用了$ this,而且还因为您将创建的实例分配给名称为*的类成员包含在 $ instance中,这似乎是空......不知道这实际上是如何工作的......

self should be used with static members (use $this->connected instead of self::connected ). self应与静态成员一起使用(使用$this->connected而不是self::connected )。

UPDATE 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. x3ro是正确的。 You also need the $this->connected syntax at the end: 最后还需要$ this-> connected语法:

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. 如果在使用$ this-> connected时收到错误消息,那是因为您的函数不是类上的方法,而是全局函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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