简体   繁体   English

PHP MySQLi OOP中“ - >”和“::”之间的区别

[英]Difference between “->” and “::” in PHP MySQLi OOP

Can anyone tell the difference between mysqli->commit and mysqli::commit ? 谁能说明mysqli->commitmysqli::commit之间的区别?

The header in this page is mysqli::commit , but in examples they use mysqli->commit . 这个页面中的标题是mysqli::commit ,但在示例中它们使用mysqli->commit

-> is used when referring to a member of an object. ->在引用对象的成员时使用。

:: is the Scope Resolution Operator and is used to refer to a static member of a Class. ::范围解析运算符 ,用于引用类的静态成员。

Consider the following class: 考虑以下课程:

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

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

You would call the function buzz() using -> : 你可以使用->调用函数buzz()

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

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): 但是会使用::来调用functon fizz() ,因为它是一个静态成员(一个不需要调用类实例的成员):

FooBar::fizz();

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. 此外,在我们讨论静态成员与实例化成员之间的区别时,您不能使用$this来引用静态成员中的当前实例。 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). 您可以使用self代替(没有前导$ ),它指的是当前类,或parent ,如果你想引用父类,或者如果你有PHP 5.3.0工作的乐趣, static (它允许后期静态绑定)。


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: 仍然使用相同的示例,引用函数buzz()的文档条目将使用以下标头:

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();

:: is used for static methods . ::用于静态方法

-> is used for method of an object if you already have the instance. ->如果已有实例,则用于对象的方法。

If you have an instance of an object, you use -> to refer to a method inside this instance: 如果您有一个对象的实例,则使用 - >来引用此实例中的方法:

$foo = new Foo();
$foo->bar();

Using :: calls a static method without having to create an instance of the object: 使用::调用静态方法而不必创建对象的实例:

Foo::bar();

A static method cannot refer to an it's current instance through $this , but can refer to itself (current class) by using self . 静态方法不能通过$this引用它的当前实例,但可以使用self引用自身(当前类)。

:: specifies a static (class) method, which is callable without actually instantiating an object. ::指定一个静态(类)方法,该方法可调用而无需实际实例化对象。 -> specifies an instance (object) method, for which you need an object instantiated to be able to use. ->指定一个实例(对象)方法,您需要一个实例化的对象才能使用。

So for example, if you had a variable $m which was an instance of class mysqli , you would call commit by saying $m->commit() , or you could call commit statically by saying MySQLi::commit() 因此,举例来说,如果你有一个变量$m这是类的一个实例mysqli ,你会打电话commit$m->commit()或者你可以说静态调用commit MySQLi::commit()

::访问类的函数而不实例化对象。

The -> operator is for object properties . ->运算符用于对象属性

The :: operator is for class properties . ::运算符用于类属性

在mysqli-> commit中,mysqli是MySQLi的一个实例,在mysqli :: commit中调用一个静态方法

mysqli-> commit是一个公共函数,mysqli :: commit是一个静态函数,两个是mysqli类的php对象表示法。

usually in php.net documentation :: means that this class has that method. 通常在php.net文档中::表示此类具有该方法。 For pratical usage you must follow the example so use the -> sintax. 对于实际使用,您必须遵循示例,因此请使用 - > sintax。

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

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