Home  >  Article  >  Backend Development  >  Attributes and class methods in PHP5 classes_PHP tutorial

Attributes and class methods in PHP5 classes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:00896browse

This article introduces in detail the attributes and class methods in PHP5 classes. Friends who need to learn can refer to the information.

PHP5 class methods

Process: A process is a sequence of statements defined when programming to complete a specified operation.

Function: A function has a return value and is also a defined sequence of statements.

Method: In the object-oriented concept, a sequence of statements in a class.

Generally speaking, in object-oriented concepts, the two terms function and method are common.

Read attributes through methods
The following example sets the property to private and declares the public getName() method to obtain the value of the property $name. Calling the getName() method will return the value of $name through return $this->name.

The code is as follows Copy code
 代码如下 复制代码

class Person

{

private $name = “NoName”; //private成员$name

public function getName() {

return $this->name;

}

}

$newperson = new Person();

echo ” ” . $newperson->getName();

?>

 

class Person

{

private $name = “NoName”; //private member $name


public function getName() {

return $this->name;
}

}
 代码如下 复制代码

class Person

{

private $a;

function setA($_a) {

$thia->a = $_a;

}

function getA() {

return $this->a;

}

}

$newperson = new Person();

$newperson->setA(100);

echo $newperson->getA();

?>

 

$newperson = new Person();

 代码如下 复制代码

class A

{

public $name = “tom”;

}

class Person

{

private $a;

function setA($_a) {

$this->a = $_a;

}

function getA() {

return $this->a;

}

}

$a1 = new A();

$p = new Person();

$p->setA($a1);

echo $p->getA()->name;

?>

echo ” ” . $newperson->getName(); ?>
Note: Here, when calling local properties inside the method, use $this->name to get the properties. In this example, the public getName() method is set, that is, the user can only obtain $name, but cannot change its value. This is the benefit of encapsulation. Encapsulation refers to the mechanism that bundles the state information (properties) and behavior (methods) of an object into a logical unit. In PHP5, data is encapsulated and declared as private, and then one or more public methods are provided to operate the attribute to achieve the following purposes: Prevent unauthorized access to encapsulated data. Users can only access data through pre-customized methods, and can easily add control logic to limit unreasonable operations on attributes; Helps ensure data integrity; Easy to modify and enhance the maintainability of the code; Method parameters Variables can be passed inside the method through parameters when the method is defined. In line 5 below, the method parameter $_a is defined when defining the method. When using this method, you can pass parameter variables into the method. Variables received within a method are local variables and are only valid within the method. You can apply a variable to the entire object by passing its value to a property.
The code is as follows Copy code
<🎜>class Person<🎜> <🎜>{<🎜> <🎜>private $a;<🎜> <🎜>function setA($_a) {<🎜> <🎜>$thia->a = $_a; } function getA() { return $this->a; } } $newperson = new Person(); $newperson->setA(100); echo $newperson->getA(); ?>
If this method is declared to have parameters and no parameters are passed when calling this method, or the number of parameters is insufficient, the system will report an error. If the number of parameters exceeds the number of parameters defined by the method, PHP will ignore the excess parameters and will not report an error. Default values ​​can be set for parameters when the function is defined. When calling the method, if no parameters are passed, this parameter variable will be filled with default values.
The code is as follows Copy code
<🎜>class A<🎜> <🎜>{<🎜> <🎜>public $name = “tom”;<🎜> <🎜>}<🎜> <🎜>class Person<🎜> <🎜>{<🎜> <🎜>private $a;<🎜> <🎜>function setA($_a) {<🎜> <🎜>$this->a = $_a; } function getA() { return $this->a; } } $a1 = new A(); $p = new Person(); $p->setA($a1); echo $p->getA()->name; ?>

Usage of attributes: Call the attribute of the object pointed to by the variable by referencing the -> symbol of the variable.

Call a property of the same object inside a method via the $this-> notation.

The code is as follows
 代码如下 复制代码

class Person

{

public $name = “NoName”; //定义public属性$name

public $age = 20; //定义public属性$age

}

$p = new Person(); //创建对象

echo ” ” . $p->name; //输出对象$p的属性$name

echo “
”;

echo ” ” . $p->age; //输出$age属性

?>

 

Copy code


class Person

{

public $name = “NoName”; //Define public attribute $name
代码如下 复制代码

class Person

{

public $name = “NoName”; //公共变量$name

public $age = 20; //公共变量$age

}

$p = new Person();

$p->name = “Tom”; //我是Tom

$p->age = 25 ; //年龄25

echo ” ” . $p->name; //输出名字

echo “
”;

echo ” ” . $p->age; //年龄

?>

 

public $age = 20; //Define public attribute $age

}

$p = new Person(); //Create object

echo ” ” . $p->name; //Output the attributes of object $p $name

echo “
”;

echo ” ” . $p->age; //Output $age attribute

?>

Attributes in PHP5 classes

We can also change the value of the attribute. Of course, it should be noted that changing the value of the attribute is modified through public

Let’s modify this example:

The code is as follows Copy code

class Person

public $name = “NoName”; //Public variable $name public $age = 20; //Public variable $age } $p = new Person(); $p->name = “Tom”; //I am Tom $p->age = 25; //Age 25 echo ” ” . $p->name; //Output name
echo “
”; echo ” ” . $p->age; //Age ?> Create a Person object and change the properties of this object. Name it and see its name. You are the god of the Person object in the machine. According to the rules you defined, this real Person object in memory is created, and it has properties that can be changed. Initial value of attribute In PHP5, you can not set an initial value in the attribute definition, or assign the following red type initial value. There are 8 simple types in PHP, namely: Four scalar types: Boolean integer Float (floating point number, also called "double") String Two compound types: Array Object Finally there are two special types: resource http://www.bkjia.com/PHPjc/628913.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628913.htmlTechArticleThis article introduces in detail the attributes and class methods in PHP5 classes. Friends who need to learn can refer to the news. . PHP5 class method process: A process is a statement sequence defined when programming...
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