Home  >  Article  >  Backend Development  >  php object-oriented encapsulation

php object-oriented encapsulation

不言
不言Original
2018-06-06 09:46:532354browse

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.

Definition:

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.

Access control (visibility constraints)

Achieved by adding keywords in front.

php object-oriented encapsulation

Example

<?php

class MyClass
{
    public $a =&#39;public&#39;;
    protected $b =&#39;protected&#39;;
    private $c = &#39;private&#39;;

    public function test(){
        // 类自身调用
        //echo $this->a;
        //echo $this->b;
        //echo $this->c;
    }
}

// 实例化
$c1 = new MyClass();

// 类外 以对象形式调用
echo  $c1 -> a;  // public
echo &#39;<br/>&#39;;
// echo  $c1 -> b;   报错
// echo  $c1 -> c;   报错

// 调用方法
$c1 -> test();

?>

Definition:

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.

Access control (visibility constraints)

Achieved by adding keywords in front.

php object-oriented encapsulation

Example

<?php

class MyClass
{
    public $a =&#39;public&#39;;
    protected $b =&#39;protected&#39;;
    private $c = &#39;private&#39;;

    public function test(){
        // 类自身调用
        //echo $this->a;
        //echo $this->b;
        //echo $this->c;
    }
}

// 实例化
$c1 = new MyClass();

// 类外 以对象形式调用
echo  $c1 -> a;  // public
echo &#39;<br/>&#39;;
// 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!

Statement:
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