PHP Notes: Detailed explanation based on object-oriented design_PHP tutorial

WBOY
Release: 2016-07-21 15:10:07
Original
736 people have browsed it

public means global and can be accessed by subclasses inside and outside the class;

Copy code The code is as follows:

< ?php

class Test{
public $name='Janking',
$sex='male',
$age=23;

function __construct() {
                           echo $this->age.'
'.$this->name.'
'.$this->sex.'


';
$P->age=100;
$P->name="Rainy";
$P->sex= "female";
$P->func();
?>
Public


private means private and can only be used within this class;



Copy code

The code is as follows:

class Test{ private $name=' Janking', $sex='male', $age=23;
function __construct(){
$this->funcOne();
}

function func(){
echo $this->age.'
'.$this->name.'
'.$this-> sex.'
';
}

>name.'
'.$this->sex.'
';
                 }                  Test();
echo '

';
$P->func();
$P->age=100; // Cannot access private property Test::$age
$P->name="Rainy"; // Cannot access private property Test::$name
$P->sex="female"; // Cannot access private property Test::$female
$P->funcOne(); // Call to private method Test::funcOne() from context ''
?>
Private


protected means protected and can only be accessed in this class or subclass or parent class; Magic methods related to encapsulation:


__set(): It is a method that is automatically called when directly setting the property value of a private member

__get(): It is a method that is automatically called when it is directly getting the value of a private member property

__isset(); is a method that is automatically called when isset directly to check whether the private attributes in the object exist

__unset(); is a method that is automatically called when directly unset deletes the private attributes in the object



http://www.bkjia.com/PHPjc/327164.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/327164.html

TechArticle

public means global and can be accessed by both internal and external subclasses of the class; copy the code as follows: ?php class Test{ public $name='Janking', $sex='male', $age=23; function __construct(){ echo...

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!