Detailed explanation of bugs in PHP permission control

小云云
Release: 2023-03-22 21:14:02
Original
1367 people have browsed it

This article mainly shares with you the detailed explanation of bugs in PHP permission control, mainly in the form of text and code. I hope it can help everyone.

class Human {
    private $money = 1000;    public function getMoney($people) {
        return $people->money;
    }    public function setMoney($people) {
        $people->money -= 500;
    }
}$zhangsan = new Human();$lisi = new Human();//echo $lisi->money; //报错// 让李四去打探张三的钱echo $lisi->getMoney($zhangsan),&#39;<br >&#39;; //1000// 让李四去改变张三的钱$lisi->setMoney($zhangsan); //减500echo $lisi->getMoney($zhangsan),&#39;<br >&#39;; //剩500
Copy after login
Copy after login
/* 
按理来说,李四不应该有权直接引用 张三私有的
但是,在上面的代码中,李四却显然引用和改张三的钱. 
这是因为: 
PHP在实现上,并不是以对象为单位来控制的权限. 
而是以类为单位,来控制的权限.
所以我们强调的是类内,类外,而不是对象内,对象外. 
*/
/* 
$lisi–>类–>Human类 
$lisi->setMoney()函数,也在Human类中, 
在同一个类内部,可以调用.
这也说明了,确实是以类为单位的. 
*/
Copy after login
class Human {
    private $money = 1000;    public function getMoney($people) {
        return $people->money;
    }    public function setMoney($people) {
        $people->money -= 500;
    }
}$zhangsan = new Human();$lisi = new Human();//echo $lisi->money; //报错// 让李四去打探张三的钱echo $lisi->getMoney($zhangsan),&#39;<br >&#39;; //1000// 让李四去改变张三的钱$lisi->setMoney($zhangsan); //减500echo $lisi->getMoney($zhangsan),&#39;<br >&#39;; //剩500
Copy after login
Copy after login

/*
Logically speaking, Li Si should not have the right to directly quote Zhang San’s private

However, in the above code, Li Si obviously quoted and changed Zhang San Three money.
This is because:
In the implementation of PHP, the permissions are not controlled in units of objects.
But the permissions are controlled in units of classes.

So what we emphasize is within the class and outside the class, not within the object and outside the object.
*/

/*
$lisi–>Class–>Human Class
The $lisi->setMoney() function is also in the Human class.
It can be called within the same class.

This also shows that it is indeed based on the class.
*/

Related recommendations:

PHP object-oriented private permission control_PHP tutorial

The above is the detailed content of Detailed explanation of bugs in PHP permission control. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!