“-"和“-"之间的区别是:和"::"在PHP MySQLi OOP中 [英] Difference between "->" and "::" in PHP MySQLi OOP

查看:63
本文介绍了“-"和“-"之间的区别是:和"::"在PHP MySQLi OOP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能说出mysqli->commitmysqli::commit之间的区别?

Can anyone tell the difference between mysqli->commit and mysqli::commit?

此页面中的标头是mysqli::commit,但在示例中它们是使用mysqli->commit.

The header in this page is mysqli::commit, but in examples they use mysqli->commit.

推荐答案

->.

::范围解析运算符并且用于引用Class的静态成员.

:: is the Scope Resolution Operator and is used to refer to a static member of a Class.

请考虑以下课程:

class FooBar {
    public static function fizz() {
        echo "Fizz";
    }

    public function buzz() {
        echo "Buzz";
    }
}

您将使用->调用函数buzz():

$myFooBar = new FooBar();
$myFooBar->buzz();

但是会使用::来调用functon fizz(),因为它是静态成员(不需要调用类实例的成员):

But would use :: to call the functon fizz(), as it is a static member (a member which doesn't require an instance of the class to be called):

FooBar::fizz();


此外,尽管我们在讨论静态成员与实例化成员之间的区别,但是您不能使用$this引用 static中的当前实例成员.您可以使用 self (不能以$开头)指的是当前类,或者 parent 想引用父类,或者如果您愿意使用PHP 5.3.0,请


Also, while we are talking about the difference between static members versus instantiated members, you cannot use $this to refer to the current instance within static members. You use self instead (no leading $) which refers to the current class, or parent if you want to refer to the parent class, or if you have the pleasure of working with PHP 5.3.0, static (which allows for late static binding).

文档使用::引用类中的函数,因为标头中的类名称不是该类的实例.仍然使用相同的示例,引用功能buzz()的文档条目将使用以下标头:

The documentation uses :: to refer to a function inside a class as the class name in the header is not an instance of the class. Still using the same example, a documentation entry referring to the function buzz() would use the following header:

FooBar::buzz

但是除非文档中指定它是静态成员,否则您将需要在实例上使用->来调用它:

But unless the documentation specifies it's a static member, you will need to use -> on an instance to call it:

$myFooBar = new FooBar();
$myFooBar->buzz();

这篇关于“-"和“-"之间的区别是:和"::"在PHP MySQLi OOP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆