search
Homephp教程PHP开发PHP object-oriented field declaration and use

PHP object-oriented field declaration and use

Dec 15, 2016 am 09:51 AM
Fieldobject-oriented

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


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool