开发者

What does "->" mean/refer to in PHP? [duplicate]

开发者 https://www.devze.com 2023-01-31 11:06 出处:网络
This question already has answers here: Reference — What does this symbol mean in PHP? (24 answers) Closed 8 years ago.
This question already has answers here: Reference — What does this symbol mean in PHP? (24 answers) Closed 8 years ago.

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 ) :开发者_开发技巧 ?>   


-> 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


Firstly you should understand the following. In PHP and many other languages we have the following types of entites:

  • Variables
  • Arrays
  • Objects

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.


It's like the period (.) in JavaScript and Java. It is just a simple access operator.


-> is the used to access methods and attributes of an object. See the PHP manual on classes and objects.


It accesses a member of the object on the left with the name on the right.


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.


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


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.


<?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.


-> 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.


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.


$object->property is used to access the property of any object.

0

精彩评论

暂无评论...
验证码 换一张
取 消