可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
Reference — What does this symbol mean in PHP?
17 answers
What does ->
mean/refer to in PHP?
In the following from WordPress, I know what the if
statement does, for example, but what does the ->
do?
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
回答1:
->
accesses a member of an object. So $wp_query->max_num_pages
is accessing the field max_num_pages
in the object $wp_query
. It can be used to access either a method or a field belonging to an object, and if you're familiar with C++ or Java, it's equivalent to myObject.myField
回答2:
Firstly you should understand the following. In PHP and many other languages we have the following types of entites:
The ->
allows you to access a method or value within an object, the same way that []
allows you to access values within an array.
A class is like a box, and within that box there is a lot of items, and each item can interact with each other as they are within the same box.
For example:
class Box
{
function firstItem()
{
}
function secondItem()
{
}
}
The above is what we call a class. It's basically a structured piece of code that does not really do anything until it becomes an object.
The object is created by using the new
keyword, which instantiates a class and creates an objects from it.
$box = new Box;
Now the above $box
, which is an object created from the Box class, has methods inside, such as firstItem()
.
These are just like functions apart from within them we have another variable called $this
and this is used to access other methods within that object.
Now to access the methods from outside the objects you have to use the operator described in your question.
$box->firstItem();
The operator ->
will allow you to execute the method from the variable $box
.
回答3:
It's like the period (.) in JavaScript and Java. It is just a simple access operator.
回答4:
-> is the used to access methods and attributes of an object. See the PHP manual on classes and objects.
回答5:
It accesses a member of the object on the left with the name on the right.
回答6:
It accesses the member of the object; $obj->prop accesses the "prop" property of whatever object is in the $obj variable.
In many other programming languages, a period is used for this purpose: obj.prop or obj.method(), for example.
回答7:
It is how PHP handles objects. Essentially, $wp_query
is an object that has methods (functions) and attributes that can be accessed through the ->
characters.
PHP didn't start with objects so you see it now as sort of an afterthought. I find ->
to be a messy way to handle it, compared to say Ruby, which was built with objects from the foundation.
You can find more at: http://php.net/manual/en/language.oop5.php
回答8:
This is very simple to understand.
In PHP we use -> to access a method/property defined inside a class.
So in your case ($wp_query->max_num_pages), you are trying the get the value of max_num_pages which is a variable of $wp_query class.
$wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.
回答9:
<?php
class Main{
private $name = 'My Name is Febri.<br/>';
private function print_name(){
echo $this -> name;
}
}
class Descend extends Main{
function print(){
$this -> print_name();
}
}
$try = new Descend;
$try -> print();
echo $try -> name;
?>
From the example above, we can not call a function which is a private print_name method. In addition, we also can not call the name variable which is set as private property.
回答10:
->
is basically used to access a filed of an object. Analogioous to attributes in Java.
for eg.
class Student {
String name;
int rollno;
}
Student.name accesses the name of a given student object.
回答11:
Use ->
to access fields, methods in an object, it is parallel to []
in array variables ($array['field']
is $object->field
). In WP you'll use it on $post
for example, since it is an object.
回答12:
$object->property
is used to access the property of any object.