PHP Learning notes One of the three object-oriented features of PHP [encapsulation] Application _php Foundation

Source: Internet
Author: User
Copy Code code as follows:

<?php
/*
* Encapsulation: One of the three main characteristics of the object-oriented
*
* 1. Is to combine the object's members (attributes, methods) into a separate unit of the same, and hide the internal details of the object as much as possible
* Access rights modifier public protected private
* Private: A member, decorated with this keyword, that can only be accessed within an object (only with $this access)
*
* Attributes can be encapsulated:
* As long as a variable is used in more than one method, the variable is declared as a member property and can be used directly in all the methods in this object.
*
* Member properties, equivalent to global variables in this object
*
* Member properties are used in methods, and the change in member property values is in fact changing the execution behavior of the method, that is, changing the function of the object
*
* Member Property if the value is not normal, the method performs the function of the leaf is not normal
*
* Role: Do not need to change or read its value outside the object
* 1. Package
* Provide a public method (which can be controlled by assigning values and values to an object member property after a method)
*
*
* Methods can also be encapsulated
*
Role
* 1. Use private decoration to make it available for internal use only
*
* 2. There are 100 methods in a class that encapsulate 95 (for another 5), and only 5 methods can be used
*
* 100 member properties, are allowed to value, can not be modified, or only can be modified, not to get the value//In this case, use the following method is more convenient
* and encapsulation-related magic methods:
*
* __set ()//Is the method that is automatically invoked when a [private] member property value is set directly
* __get ()//Is the method that is automatically invoked when a [private] member property value is obtained directly
* __isset ()//is automatically invoked when using Isset () to see if private properties exist in an object
* __unset ()//is a method that is automatically invoked when a private property in an object is deleted directly using unset ()
*
*
*
*
*
*
*
*/
Class person{
X encapsulates member properties and does not need to be changed outside the object
Private $name;
Private $age;
Private $sex;
Private __unset ($proName) {
unset ($this-> $proName);
}
is to automatically call this method if it is directly viewing the existence of a private property in an object
Use of __isset ($proName), $proName represents the property name
Private Function __isset ($proName) {
return Isset ($this-> $proName);//isset () returns whether there is a
}
function __construct ($name, $age, $sex) {
$this->name= $name;
$this->age= $age;
$this->sex= $sex;
}
This method is called automatically when a private member property is obtained
Private Function __get ($proName)
{
Controlling the value obtained
if ($proName = = "Age") {
if ($this-age>40)
return $this->age-10;
}
return $this-> $proName;
}
This method is called automatically when a private member property is set
Private Function __set ($proName, $proValue) {
$proName represents a member property name, $proValue represents a member property value
Control Settings Scope
if ($proName = = "Age") {
if ($proValue > | | $proValue <0)
Return
}
$this-> $proName = $proValue;
}
Provides a public method to set the value of a member property
function Setage ($age) {
Control age range and increase safety
if ($age > | | $age < 0)
Return
$this->age= $age;
}
Provides a public method to get the value of a member property
function Getage () {
Control the range of acquired age
if ($this->age < 30)
return $this->age;
else if ($this->age<40)
return $this->age-5;
else if ($this->age<50)
return $this->age;
Else
return $this->age-15;
Provide public method to}
function say () {
echo "My name: {$this->name}, my age: {$this->age}, my last name: {$this->sex}<br>";
Access the encapsulated run () method
$this-run ();
}
Private function Run () {
Echo ' 111111111111<br> '
}
Function Eat () {
}
destructor method
function __destruct () {
}
}
$p 1=new person ("Zhangsan", 25, "male");
$p 2=new person;
$p 3=new person;
$p 1->age=-50;//because of the age of casual access to the outside, so member properties of drug encapsulation, only security.
$p 1->setage (30);//by method to set the member property de value
$p 1->getage ();//by method to get member property de value
By adding the Home Magic Method __set ($proName, $proValue) __get ($proName), you can invoke the member property directly.
$p 1->say ()//Can call
$p 1->run ();//private method cannot call directly
Delete the name inside the $P1
unset ($p 1->name);
To determine if name exists
if (Isset ($p 1->name)) {
echo "Existence <br>";
}else{
echo "does not have this member <br>";
}
?>

Author: symbol Aurora
Source: http://zizhuyuan.cnblogs.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.