This article describes in detail the properties and class methods in the PHP5 class, and there are friends who need to learn to refer to the message.
PHP5 class method
Procedure: A procedure is a sequence of statements that is defined when the program is programmed to perform a specified operation.
Function: The function has a return value and is also a defined sequence of statements.
Method: In the object-oriented concept, a sequence of statements inside a class.
In general, in object-oriented concepts, functions and methods two nouns are universal.
Read properties by method
The following example sets the property to private and declares the public's GetName () method to get the value of the property $name, and the call to the GetName () method returns the value of the $name by returning the return $this->name.
| The code is as follows |
Copy Code |
Class Person { Private $name = "NoName"; Private member $name Public Function GetName () { return $this->name; } } $newperson = new Person (); echo "". $newperson->getname (); ?> |
Note: Here, when the method internally calls the local property, it uses the $this->name to get the property. In this example, the public GetName () method is set, that is, the user can only get $name, and cannot change his value. This is the benefit of encapsulation.
Encapsulation refers to the mechanism by which the state information (attributes) and behavior (methods) of an object are bundled into a single logical unit.
In PHP5, by encapsulating the data, declaring it private (private), and then providing one or more public methods to implement the operation of the property, for the following purposes:
Prevent unauthorized access to encapsulated data. The user can only access the data by pre-custom method, and then add the control logic to restrict the unreasonable operation of the attribute.
Help to ensure the integrity of the data;
Easy to modify, enhance the maintainability of the Code;
Parameters of the method
You can pass a variable to the inside of a method by using a parameter that is defined by the method.
In line 5th below, the method parameter $_a is defined when the method is defined. When you use this method, you can pass parameter variables to the method. The variables inside the method are local variables and are valid only within the method. You can have this variable applied to the entire object by passing the value of the variable to the 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 you declare that the method has parameters, and the method is called without passing arguments, or if the number of parameters is insufficient, the system will report an error. If the number of parameters exceeds the number of method-defined parameters, PHP ignores more parameters and does not error. You can set a default value for a parameter when the function is defined. When the method is called, if no arguments are passed, the parameter variable is populated with the 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; } } $a 1 = new A (); $p = new Person (); $p->seta ($a 1); echo $p->geta ()->name; ?> |
Use of a property: a symbol called by a reference variable that points to the property of the object.
Invokes the properties of the same object within the method, using the $this-to symbol.
| The code is as follows |
Copy Code |
Class Person { Public $name = "NoName"; Define the public property $name Public $age = 20; Define the public property $age } $p = new Person (); Creating objects echo "". $p->name; The properties of the output object $p $name echo " ”; echo "". $p->age; Output $age Properties ?> |
Properties in the PHP5 class
We can also change the value of the property, of course, notice that the value of the Change property is modified by public.
Let's revise 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'm Tom. $p->age = 25; Age 25 echo "". $p->name; Output name echo " ”; echo "". $p->age; Age ?> |
Create an object of person and change the properties of this object. Name it, and look at its name. You are the god of this person object in the machine, and according to the rules you define, this real in-memory person object is created, and it has properties that can be changed.
Initial value of the attribute
In PHP5, the attribute definition can be set without setting the initial value, or given the following red type of initial value.
There are 8 simple types in PHP, namely:
Four types of scalar:
Boolean Type (Boolean)
Integral type (integer)
Float (float) (floating-point number, also "double")
Strings (String)
Two kinds of composite types:
Arrays (Array)
Objects (object)
Finally, there are two special types:
Resources (Resource)
http://www.bkjia.com/PHPjc/628913.html www.bkjia.com true http://www.bkjia.com/PHPjc/628913.html techarticle This article describes in detail the properties and class methods in the PHP5 class, and there are friends who need to learn to refer to the message. PHP5 class method procedure: A procedure is a statement order that is defined when the program is compiled ...