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 | ||||||||||||
{ private $name = “NoName”; //private member $name
}
$newperson = new Person();
|
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(); ?> |
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
|
Copy code
|
||||||||
class Person { public $name = “NoName”; //Define public attribute $name
public $age = 20; //Define public attribute $age } $p = new Person(); //Create object 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:
class Person source:php.cn
Previous article:Array_merge program to merge two arrays in PHP_PHP tutorial
Next article:Detailed explanation of how to use PHP sprintf() function_PHP tutorial
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
Latest Articles by Author
Latest Issues
How to display the mobile version of Google Chrome
Hello teacher, how can I change Google Chrome into a mobile version?
From 2024-04-23 00:22:19
0
11
2202
There is no output in the parent window
document.onclick = function(){ window.opener.document.write('I am the output of the child ...
From 2024-04-18 23:52:34
0
1
1746
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|