search

写了一段时候PHP,回过头来在看PHP.感觉还是有很多地方不明白.
今天在看到private的时候遇到一个小问题, 就发个帖子请教一下大家!

<?php
class Father
{
    protected $name = 'Father';

    public function getName()
    {
        return $this->name;
    }
}

class Son extends Father
{
    protected $name = 'Son';
}

$son = new Son();
echo $son->getName();   //返回的是Son 

另一种情况

<?php
class Father
{
    private $name = 'Father';

    public function getName()
    {
        return $this->name;
    }
}

class Son extends Father
{
    protected $name = 'Son';

}

$son = new Son();
echo $son->getName();    //返回的确实father

这里就有一个问题, php对象属性的值, 在程序运行的过程中是怎么确定的!
有什么规则,或者说是一个怎么样的访问过程!

回复内容:

写了一段时候PHP,回过头来在看PHP.感觉还是有很多地方不明白.
今天在看到private的时候遇到一个小问题, 就发个帖子请教一下大家!

<?php
class Father
{
    protected $name = 'Father';

    public function getName()
    {
        return $this->name;
    }
}

class Son extends Father
{
    protected $name = 'Son';
}

$son = new Son();
echo $son->getName();   //返回的是Son 

另一种情况

<?php
class Father
{
    private $name = 'Father';

    public function getName()
    {
        return $this->name;
    }
}

class Son extends Father
{
    protected $name = 'Son';

}

$son = new Son();
echo $son->getName();    //返回的确实father

这里就有一个问题, php对象属性的值, 在程序运行的过程中是怎么确定的!
有什么规则,或者说是一个怎么样的访问过程!

我来说说我的看法。

getName 是一个 public 方法,那么 Son 类会继承 Father 类的这个方法,这一点没有疑问,对吧。

那么先看看第一段代码的执行思路。

Son 类实例化之后,执行 getName 方法。由于这个方法是从 Father 类继承而来的,所以实际上这个方法访问的是 Father 类的 $name 这个属性。第一段代码里 $nameprotected 类型的,在子类中这个属性被继承下来并被改写,那么 getName 就会继续去查找子类里的这个属性。

而在第二段代码的里,$name 属性已经被定义成 private 类型,所以在子类执行 getName 方法时,会先查找父类里的 $name 属性。这时候 $name 是无法被继承的,所以就返回了父类的值。

不知道我这样描述的是否详细,能让题主理解?

以往的工程里,我写的类访问级别只到 protected,从来不用 private,我觉得这样更符合面向对象的思想。

你试试看这个 https://segmentfault.com/q/1010000000133607
是不是你的也有警告提示但是因为设置的原因没有显示出来呢?
按说父类声明了 private $attr的话子类可以 private $attr,但是不能 protected $attr 或者 public $attr,否则会很混乱,类似的例子 父类 protected $attr;子类 public $attr;那返回哪个。

我发现黑客新想法的最佳来源,并非是名字里有“计算机”三个字的领域,而是来自于其他的创作领域 --《黑客与画家》21页

既然已经面向对象了,那么对象这个词也肯定也不是空穴来风,话说世界万物皆对象,也不知道当初提出面向对象的人脑子里想的是谁家的对象。这个面向对象的概念大到了保持对整个世界的同理心!!!

你看看提出面向对象的人,都有这么高的Bigger。那我们学习面向对象,也至少不应该只局限在编程里。

说道爹,就应该好好唠一唠,这个是一个拼爹的时代。
我爹在前门有套房,我爹的媳妇我叫娘,我爹外号叫光头强。

<code>class Father {
    private $fatherHouse  = '我爹在前门的四合院,房产证上只写着爹的名字';
    protected $fatherWife = "我爹的媳妇";
    public $nickname      = "光头强";
    
    public function whereFatherHouse(){
        echo $this->fatherHouse;
    }

    public function callFatherWife(){
        echo $this->fatherWife;
    }
}</code>

说到我啊,他们制造我的时候是这个样子

<code>class Me extends Father {
    protected $fatherWife = '娘';
}
$me = new Me();</code>

我爹在前门有套房

<code>$me->whereFatherHouse(); </code>

我爹的媳妇我叫娘

<code>$me->callFatherWife();</code>

我的外号叫夜夜强

<code>$me->nickname = '夜夜强';</code>

共有 谁都能给你起个外号,约束不了
私有 爹的房子,你始终改不成你的,只能炫耀下。
保护 家族中的一个属性

第一个父类name被重写了,所以就只有一个name。
第二个父类name没有被重写,所以调用父类的私有属性。

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
How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

How often should you regenerate session IDs?How often should you regenerate session IDs?Apr 23, 2025 am 12:03 AM

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

How do you set the session cookie parameters in PHP?How do you set the session cookie parameters in PHP?Apr 22, 2025 pm 05:33 PM

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

What is the main purpose of using sessions in PHP?What is the main purpose of using sessions in PHP?Apr 22, 2025 pm 05:25 PM

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How can you share sessions across subdomains?How can you share sessions across subdomains?Apr 22, 2025 pm 05:21 PM

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

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.