Home > Backend Development > PHP Tutorial > PHP object-oriented field declaration and use_PHP tutorial

PHP object-oriented field declaration and use_PHP tutorial

WBOY
Release: 2016-07-21 15:18:20
Original
914 people have browsed it

Fields are used to describe aspects of a class.

Fields are used to describe the properties of certain aspects 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 declarations are very similar to those for variable declarations; in fact, 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. An example is as follows:

Copy code The code is as follows:

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

In this example, the two fields name and wage are preceded by a scope descriptor (public or Private), which is the declaration field common practice. 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 are 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 interrelationship with that object:
Copy code The code is as follows:

$object->field

For example, the Employee class described at the beginning of this chapter includes the fields name, title and wage. If you create an Employee type object named $employee, you can reference these fields as follows:
Copy code The code is as follows:

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

When referencing a field in the class that defines the field, also use -> ; 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). So, if you were to create a method in the above Employee class that sets the name field, it would look like this:
Copy the code The code would be as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325550.htmlTechArticle field is used to describe various aspects of the class. Fields are properties used to describe certain aspects of a class. It is very similar to general PHP variables, with just some subtle differences. This section will...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template