Encapsulation of object-oriented programming in PHP

Source: Internet
Author: User
Tags access properties modifier modifiers variables

The concept of encapsulation

Encapsulation is the encapsulation of the abstracted data and the operation of the data, the data is protected internally, and other parts of the program can operate on the data only through authorized operations (member methods).

In the category of people, people's age and wages are private

<?php
Class person{
Public $name;
Private $age;
Private $salary;
function __construct ($name, $age, $salary) {
$this->name= $name;
$this->age= $name;
$this->salary= $salary;
}
}
Create a person
$p 1=new person ("John", 30,1000);
Access to John Wages
Echo $p 1->salary; An error is not available to access properties in private
Echo $p 1->name;
?>

Encapsulated access Modifiers

PHP provides three access control modifiers to control the access rights of methods and variables (properties)

Public represents the global, and the external, internal, and subclass of the class are accessible.

Protected represents a protected, only this class or subclass can be accessed;

Private means proprietary, only within this class can be used;

These three modifiers can modify variables and methods.

1 If a method does not have an access modifier, the default is public.

2) Property (variable) must specify an access modifier.

3 methods can be invoked with each other, but $->this references are required.

As shown in the following table:

Public Protected Private
Global X X
Inheriting classes X
This class

Cases:

<?php
Class person{
Public $name;
protected $age;
Private $salary;
function __construct ($name, $age, $salary) {
$this->name= $name;
$this->age= $name;
$this->salary= $salary;
}
function Showinfo () {
You can use the values of public,protected and private types in this class
echo $this->name. "| |" $this->age. "| |" $this->salary. " <br/> ";
}
}
Create a person
$p 1=new person ("John", 30,1000);
Access to John Wages
Echo $p 1->salary; An error is not available to access properties in private
echo $p 1->name. " <br/> ";
$p 1->showinfo ();
echo $p 1->age;//error because $age is protected and cannot be accessed outside of class
Echo $p 1->salary; Error, because $salary is private, outside the class can not access
?>

How do I access protected and private-decorated member variables? These variables are usually accessed through the public function in the class. You can also use the method provided by PHP

__set () and __get (); These two methods are also known as magic methods. Note: The Magic method is not recommended at this time .

__set () to assign to protected or private properties, for example:

__set ($name, $value);

__get () Gets the value of the protected or private property, for example:

__get ($name);

Cases:

<?php
Class a{
Private $n 1;
Private $n 2;
Private $n 3;
Use the __set method to manage all private properties
Public Function __set ($pro _name, $pro _val) {
$this->pro_name= $pro _val;
}
Use the __get method to manage the values of all private properties
Public Function __get ($pro _name) {
if (Isset ($pro _name)) {
return $this->pro_name;
}else{
return null;
}
}
}
$a 1=new A ();
$a 1->n1= "www.bianceng.cn";
Echo $a 1->n1; Output www.bianceng.cn
?>

Typically, you access protected and private-decorated member variables in a class by using public functions

Cases:

<?php
Class person{
Public $name;
protected $age;
Private $salary;
function __construct ($name, $age, $salary) {
$this->name= $name;
$this->age= $name;
$this->salary= $salary;
}
function Showinfo () {
You can use the values of public,protected and private types in this class
echo $this->name. "| |" $this->age. "| |" $this->salary. " <br/> ";
}
We can access protected or private variables via the public function.
function Getsalary ($user, $pass) {
if ($user = = "www.bianceng.cn" && $pass ==123) {
return $this->salary;
}else{
echo "You do not have permission to view salary";
}
}
}
Create a person
$p 1=new person ("John", 30,1000);
Access to John Wages
Echo $p 1->salary; An error is not available to access properties in private
echo $p 1->name. " <br/> ";
$p 1->showinfo ();
echo $p 1->age;//error because $age is protected and cannot be accessed outside of class
Echo $p 1->salary; Error, because $salary is private, outside the class can not access
echo $p 1->getsalary ("www.bianceng.cn", 123);
?>

Example: modifying and viewing the age of the protected in the previous example

<?php
Class person{
Public $name;
protected $age;
Private $salary;
function __construct ($name, $age, $salary) {
$this->name= $name;
$this->age= $name;
$this->salary= $salary;
}
function Showinfo () {
You can use the values of public,protected and private types in this class
echo $this->name. "| |" $this->age. "| |" $this->salary. " <br/> ";
}
We can access protected or private variables through public methods.
function Getsalary ($user, $pass) {
if ($user = = "www.bianceng.cn" && $pass ==123) {
return $this->salary;
}else{
echo "You do not have permission to view salary";
}
}
Revise age
Public Function Setage ($age) {
if ($age >=1 && $age <=120) {
$this->age= $age;
}else{
echo "<br/> Age range is not!<br/>";
}
}
View Age
Public Function Getage () {
return $this->age;
}
}
Create a person
$p 1=new person ("John", 30,1000);
Access to John Wages
Echo $p 1->salary; An error is not available to access properties in private
echo $p 1->name. " <br/> ";
$p 1->showinfo ();
echo $p 1->age;//error because $age is protected and cannot be accessed outside of class
Echo $p 1->salary; Error, because $salary is private, outside the class can not access
echo $p 1->getsalary ("www.bianceng.cn", 1203);
Revise age
$p 1->setage (75);
echo "<br/>". $p 1->getage ();
?>

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.