问题:有如下代码:
class Far
{
protected $arr;
protected function init() {
foreach ($this->arr as $k => $val) {
$this->$k = $val;
}
}
public function __construct() {
$this->init();
}
public function __set($name, $val) {
$this->$name = $val;
}
}
class Son extends Far
{
protected $a;
public function __construct() {
$this->arr = array(
'a' => '1',
);
parent::__construct();
}
}
$obj = new Son();
print_r($obj);
问:为什么$obj输出的结果中,a不是1,而是null.
Son Object
(
[a:protected] => 1
[arr:protected] => Array
(
[a] => 1
)
)
问题2:如果把上述代码中,子类的private $a 改成protected $a 或public $a,则输出:
Son Object
(
[a:protected] => 1
[arr:protected] => Array
(
[a] => 1
)
[bb] => 1
)
为什么?
回复讨论(解决方案)
不明白你在说什么?
你打印的结果不是正确的吗?
纠正一下,private时打印的结果如下:
Son Object( [a:Son:private] => [arr:protected] => Array ( [a] => 1 ) [bb] => 1)
son::a 是私有的,far:: init 访问不到他
son::a 是私有的,far:: init 访问不到他
不是有个__set魔术方法么?
__set 不是 far 的吗?
__set 不是 far 的吗?
子类不是继承了它么?
$Son->bb = 1;
可以证明 Son得到了Far的__set
不错,方法是继承了,但权限并没有因继承而改变
老婆是你的,并不能说你老子就能碰
不错,方法是继承了,但权限并没有因继承而改变
老婆是你的,并不能说你老子就能碰
是的,那么__set中的$this不是指向的当前实例对象,也就是Son的一个实例哦。而__set的作用就是可以用于访问private或不存在的成员,
代?不完整啊,看了半天,都?看到有$bb。
代?不完整啊,看了半天,都?看到有$bb。
Sorry,$bb是我外部加的,如下:
$obj = new Son();$obj->bb = 1;print_r($obj);
private 是私有属性,只能自己内部调用,实例都不可以调用。
当然Far不可以赋值,正常的。
private 是私有属性,只能自己内部调用,实例都不可以调用。
当然Far不可以赋值,正常的。
呃,我的意思是,__set, __get方法其实就是用来访问其中不存在或私有private的成员的方法。手册中如下说明:
“在给不可访问属性赋值时,__set() 会被调用。”
参见:http://www.php.net/manual/zh/language.oop5.overloading.php
你的 __set 方法是定义在 Far 中的,所以他不能访问 Son 的私有属性
这样写就可以了
class Far { protected $arr; protected function init() { foreach ($this->arr as $k => $val) { $this->$k = $val; } } public function __construct() { $this->init(); } public function __set($name, $val) { $this->$name = $val; }}class Son extends Far { private $a; public function __construct() { $this->arr = array( 'a' => '1', ); parent::__construct(); } public function __set($name, $val) { $this->$name = $val; }}$obj = new Son();print_r($obj);Son Object
(
[a:Son:private] => 1
[arr:protected] => Array
(
[a] => 1
)
)
你的 __set 方法是定义在 Far 中的,所以他不能访问 Son 的私有属性
这样写就可以了
class Far { protected $arr; protected function init() { foreach ($this->arr as $k => $val) { $this->$k = $val; } } public function __construct() { $this->init(); } public function __set($name, $val) { $this->$name = $val; }}class Son extends Far { private $a; public function __construct() { $this->arr = array( 'a' => '1', ); parent::__construct(); } public function __set($name, $val) { $this->$name = $val; }}$obj = new Son();print_r($obj);Son Object
(
[a:Son:private] => 1
[arr:protected] => Array
(
[a] => 1
)
)
是的,我之所以写在父类中,就是不想在每个子类中写一个__set,按您所说的是没有问题的。不过让我不明白的是,即然它能从父类得到这个__set的方法。与写在自己内部的__set方法,在操作成员时,倒底有什么不同的。
私有的是不可侵犯的
仔细品味#7的第二句话
私有的是不可侵犯的
仔细品味#7的第二句话
有一点我不明白的是,如果我在__set打印当前$this,这里我得到的是子类对象。那接着$this->name不就是子类对象的方法操作自己的成员么?为可这里权限却没有了。
要说多少遍你才能转过弯来呢?
私有的属性,只能被他所在类定义的方法访问
请注意类和对象的区别
要说多少遍你才能转过弯来呢?
私有的属性,只能被他所在类定义的方法访问
请注意类和对象的区别
如果你这样说,我就能理解了。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools