PHP object-oriented private permission control_PHP tutorial

WBOY
Release: 2016-07-14 10:11:20
Original
1276 people have browsed it

/***
====Notes Section====
Permission modifiers
Function: Used to illustrate the permission characteristics of attributes/methods
Write in front of properties/methods
There are 3 permission modifiers
private, the most strictly protected
protected
public , the least protected
Question:
Where can the properties/methods modified by public be accessed?
Where can private modified properties/methods be accessed?
How to determine whether a property/method has permission to access?
Answer: It depends on the location during the visit!
Private attributes/methods can only be accessed within the braces {} of the class definition
Public properties can be accessed from anywhere

***/


[php]
class human{

public $mood='';// Mood, public
private $money=500;//Money, private

public function getmoney(){
Return $this->money;

//Define private secret method
private function secret(){
         
echo 'I stole a piece of candy that day';
}
//Tell me your secret method
public function tellme(){
 
$this->secret();
}

}

$lisi=new human();
$lisi->mood='happay';

echo $lisi->mood,'
';//happay

echo $lisi->getmoney(),'
';//500

//echo $lisi->money=300;//The object cannot call private properties
//Fatal error: Cannot access private property human::$money in C:wampwwwphpprivate.php on line 31

//$lisi->secret();//Objects cannot call private methods
//Fatal error: Call to private method human::secret() from context '' in C:wampwwwphpprivate.php on line 32

$lisi->tellme(); // Yes, because it is called through line 17, that is, within the class.

/*
Summary: private permission control
Can only be called within {} of the class,
Once you get out of {}, no one can move you.
*/

?>

class human{

public $mood='';// Mood, public
private $money=500;//Money, private

public function getmoney(){
return $this->money;
}
//Define private secret method
private function secret(){

echo 'I stole a piece of candy that day';
}
//Tell me your secret method
public function tellme(){

$this->secret();
}

}

$lisi=new human();
$lisi->mood='happay';

echo $lisi->mood,'
';//happay

echo $lisi->getmoney(),'
';//500

//echo $lisi->money=300;//The object cannot call private properties
//Fatal error: Cannot access private property human::$money in C:wampwwwphpprivate.php on line 31

//$lisi->secret();//Objects cannot call private methods
//Fatal error: Call to private method human::secret() from context '' in C:wampwwwphpprivate.php on line 32

$lisi->tellme(); // Yes, because it is called through line 17, that is, within the class.

/*
Summary: private permission control
Can only be called within {} of the class,
Once you get out of {}, no one can move you.
*/

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477340.htmlTechArticle/*** ====Notes section==== Permission modifier function: Used to describe attributes/ The permission characteristics of the method are written in front of the attribute/method. There are three permission modifiers: private, private, and the most protected...
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