Home > php教程 > PHP开发 > body text

PHP object-oriented field declaration and use

黄舟
Release: 2016-12-15 09:51:29
Original
1056 people have browsed it

Fields are used to describe the properties of a certain aspect of a class. It is very similar to a regular PHP variable, with some subtle differences, which are described in this section. This section also discusses how to declare and use fields, and the next section explains how to use field scopes to restrict access.

Declaring fields
The rules for field declaration are very similar to the rules for variable declaration; in fact, it can be said that there is no difference. Because PHP is a loosely typed language, fields don't even need to be declared; they can be created and assigned values ​​from class objects at the same time, but this is rarely done. Instead, common practice is to declare fields at the beginning of the class. At this point, you can assign an initial value to the field. The example is as follows:

. The code is as follows:


class Employee
{
public $name="John";
private $wage;
}


In this example, the two fields name and wage are preceded by There are scope descriptors (public or Private), which is a common practice when declaring fields. After declaration, each field is available within the scope indicated by the scope descriptor. If you don’t understand what scope does for class fields, don’t worry, we’ll cover that later.

Using fields
Unlike variables, fields should be referenced using the -> operator instead of using the dollar sign. Furthermore, because a field's value is generally unique to a given object, it has the following correlation with that object:

. The code is as follows:


$object->field


For example, in this chapter Begin by describing the Employee class including the fields name, title and wage. If you create an Employee type object named $employee, you can reference these fields as follows:

. The code is as follows:


$employee->name
$employee->title
$employee->wage


When referencing a field in the class that defines the field, you also need to use the -> operator, but instead of using the corresponding class name, use the $this keyword. $this indicates that you want to refer to a field in the current class (the class in which the field you want to access or operate is located). Therefore, if you want to create a method to set the name field in the above Employee class, it will be as follows:

. The code is as follows:


function setName($name)
{
$this->name=$name ;
}

The above is the declaration and use of object-oriented fields in PHP. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template