Home > Article > Backend Development > php object-oriented encapsulation
This article mainly introduces the object-oriented encapsulation of PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.
Hidden objects The attributes and implementation details only provide public calls to the outside world to control the access level of reading and modifying attributes in the program.
Achieved by adding keywords in front.
<?php class MyClass { public $a ='public'; protected $b ='protected'; private $c = 'private'; public function test(){ // 类自身调用 //echo $this->a; //echo $this->b; //echo $this->c; } } // 实例化 $c1 = new MyClass(); // 类外 以对象形式调用 echo $c1 -> a; // public echo '<br/>'; // echo $c1 -> b; 报错 // echo $c1 -> c; 报错 // 调用方法 $c1 -> test(); ?>
Hide the properties and implementation details of the object, only provide public calls to the outside world, and control them in the program Read and modify access levels for properties.
Achieved by adding keywords in front.
<?php class MyClass { public $a ='public'; protected $b ='protected'; private $c = 'private'; public function test(){ // 类自身调用 //echo $this->a; //echo $this->b; //echo $this->c; } } // 实例化 $c1 = new MyClass(); // 类外 以对象形式调用 echo $c1 -> a; // public echo '<br/>'; // echo $c1 -> b; 报错 // echo $c1 -> c; 报错 // 调用方法 $c1 -> test(); ?>
Related recommendations:
php object-oriented classes and instantiated objects
Basic concepts of object-oriented php
The above is the detailed content of php object-oriented encapsulation. For more information, please follow other related articles on the PHP Chinese website!