I understand that there are two ways to access a PHP class - "::" and "->". Sometime one seems to work for me, while the other doesn't, and I don't understand why.
我知道有兩種方法可以訪問PHP類 - “::”和“ - >”。有時一個似乎適合我,而另一個沒有,我不明白為什么。
What are the benefits of each, and what is the right situation to use either?
每種方法的好處是什么,使用哪種方式的正確方法是什么?
6 个解决方案
#1
52
Simply put, :: is for class-level properties, and -> is for object-level properties.
簡單地說,::用於類級屬性, - >用於對象級屬性。
If the property belongs to the class, use ::
如果屬性屬於該類,請使用::
If the property belongs to an instance of the class, use ->
如果屬性屬於該類的實例,請使用 - >
class Tester
{
public $foo;
const BLAH;
public static function bar(){}
}
$t = new Tester;
$t->foo;
Tester::bar();
Tester::BLAH;
#2
8
The "::" symbol is for accessing methods / properties of an object that have been declared with the static keyword, "->" is for accessing the methods / properties of an object that represent instance methods / properties.
“::”符號用於訪問已使用static關鍵字聲明的對象的方法/屬性,“ - >”用於訪問表示實例方法/屬性的對象的方法/屬性。
#3
3
When you declare a class, it is by default 'static'. You can access any method in that class using the :: operator, and in any scope. This means if I create a lib class, I can access it wherever I want and it doesn't need to be globaled:
聲明一個類時,它默認為“static”。您可以使用::運算符在任何范圍內訪問該類中的任何方法。這意味着如果我創建一個lib類,我可以在任何我想要的地方訪問它,它不需要被全局化:
class lib
{
static function foo()
{
echo "hi";
}
}
lib::foo(); // prints hi
Now, when you create an instance of this class by using the new keyword, you use -> to access methods and values, because you are referring to that specific instance of the class. You can think of -> as inside of. (Note, you must remove the static keyword) IE:
現在,當您使用new關鍵字創建此類的實例時,使用 - >來訪問方法和值,因為您指的是該類的特定實例。你可以想到 - >作為內部。 (注意,你必須刪除靜態關鍵字)IE:
class lib
{
function foo()
{
echo "hi";
}
}
$class = new lib;
$class->foo(); // I am accessing the foo() method INSIDE of the $class instance of lib.
#4
3
Php can be confusing in this regard you should read this.
Php在這方面可能會令人困惑,你應該閱讀這篇文章。
What's also confusing is that you can call non static functions with the :: symbol. This is very strange when you come from Java. And it certainly surprised me when I first saw it.
令人困惑的是,您可以使用::符號調用非靜態函數。當你來自Java時,這很奇怪。當我第一次看到它時,它確實讓我感到驚訝。
For example:
例如:
class Car
{
public $name = "Herbie
";
public function drive()
{
echo "driving
";
}
public static function gas()
{
echo "pedal to the metal
";
}
}
Car::drive(); //will work
Car::gas(); //will work
$car = new Car();
$car->drive(); // will work
$car->gas(); //will work
echo $car->name; // will work
echo Car::$name; // wont work error
As you can see static is very loose in php. And you can call any function with both the -> and the :: symbols. But there is a difference when you call with :: there is no $this reference to an instance. See example #1 in the manual.
正如你可以看到靜態在php中非常松散。您可以使用 - >和::符號調用任何函數。但是當你調用::沒有$ this引用實例時會有區別。請參閱手冊中的示例#1。
#5
1
It should also be noted that every static function can also be called using an instance of the class but not the other way around.
還應該注意,每個靜態函數也可以使用類的實例調用,但不能相反。
So this works:
這樣可行:
class Foo
{
public static function bar(){}
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //works
And this doesn't:
而這不是:
class Foo
{
protected $test="fddf";
public function bar(){ echo $this->test; }
}
$f = new Foo();
$f->bar(); //works
Foo::bar(); //fails because $this->test can't be accessed from a static call
Of course you should restrict yourself to calling static methods in a static way, because instantiating an instance not only costs memory but also doesn't make much sense.
當然,您應該限制自己以靜態方式調用靜態方法,因為實例化實例不僅會花費內存,而且也沒有多大意義。
This explanation was mainly to illustrate why it worked for you some of the times.
這個解釋主要是為了說明為什么它在某些時候適合你。
#6
0
采購WikiPedia - Class
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint describes the state and behavior that the created objects all share. An object created by a class is an instance of the class, and the class that created that instance can be considered as the type of that object, e.g. a type of an object created by a "Fruit" class would be "Fruit".
在面向對象的編程中,類是一種編程語言構造,用作創建對象的藍圖。此藍圖描述了創建的對象共享的狀態和行為。由類創建的對象是類的實例,並且創建該實例的類可以被視為該對象的類型,例如,由“Fruit”類創建的對象類型將是“Fruit”。
The :: operator accesses class methods and properties which are defined in php using the static keyword. Class const are also accessed using ::
::運算符使用static關鍵字訪問php中定義的類方法和屬性。使用::也可以訪問類const
The -> operator accesses methods and properties of an Instance of the class.
- >運算符訪問類的實例的方法和屬性。
If the function operates on an instance, you'll be using ->. If it operates on the class itself, you'll be using ::
如果函數在實例上運行,您將使用 - >。如果它在類本身上運行,你將使用::
Another use of :: would be when you want to call your parent functions. If one class inherits another - it can override methods from the parent class, then call them using parent::function()
::的另一個用途是當你想要調用你的父函數時。如果一個類繼承另一個類 - 它可以覆蓋父類的方法,然后使用parent :: function()調用它們