PHP“无法访问受保护的属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16197362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:42:00  来源:igfitidea点击:

PHP "cannot access protected property"

phpoop

提问by Nextar

This is my first OOP program, so please don't be mad at me :) The problem is that I've got the following error:

这是我的第一个 OOP 程序,所以请不要生我的气:) 问题是我遇到了以下错误:

Cannot access protected property Code::$text in D:\xampp\htdocs\php\OOP\coder_class.php on line 47

无法访问第 47 行 D:\xampp\htdocs\php\OOP\coder_class.php 中的受保护属性 Code::$text

The program simply codes a string and decodes it. I'm not sure if this is a good example to learn OOP.

该程序只是对字符串进行编码并对其进行解码。我不确定这是否是学习 OOP 的好例子。

<?php
class Code
{
    // eingabestring
    protected $text;

            public function setText($string)
            {
                $this->text = $string;
            }

            public function getText()
            {
                echo $this->text;
            }
}

class Coder extends Code
{
    //Map for the coder
    private $map = array(
        '/a/' => '1',
        '/e/' => '2',
        '/i/' => '3',
        '/o/' => '4',
        '/u/' => '5');

            // codes the uncoded string
    public function coder() 
    {
        return preg_replace(array_keys($this->map), $this->map, parent::text);      
    }
}

class Decoder extends Code
{
    //Map for the decoder
    private $map = array(
    '/1/' => 'a',
    '/2/' => 'e',
    '/3/' => 'i',
    '/4/' => 'o',
    '/5/' => 'u');

            // decodes the coded string
            public function decoder()
    {
        return preg_replace(array_keys($this->map), $this->map, parent::text);      
    }
}

$text = new code();
    $text -> setText("ImaText");
    $text -> coder();
    $text -> getText();

?>

?>

Can some help me fixing this . Am new to PHP .

有人可以帮我解决这个问题。我是 PHP 新手。

回答by álvaro González

Relevant code:

相关代码:

class Code
{
    protected $text;
}
$text = new code();
echo $text->text;

The property is not public, thus the error. It works as advertised.

该属性不是公共的,因此是错误的。它像宣传的那样工作。

回答by Sammitch

With:

和:

protected $text;

And:

和:

echo $text->text;

Is why you get the error. protectedmeans that only descendants of the Codeclass can access that property, ie. Coderand Decoder. If you want to access it via $text->textit must be public. Alternatively, just write a getText()method; you've already written the setter.

这就是为什么你得到错误。protected意味着只有Code该类的后代才能访问该属性,即。CoderDecoder。如果你想通过$text->text它访问它必须是public. 或者,只需编写一个getText()方法;您已经编写了 setter。

Side note: the public, private, and protectedkeywords have virtually nothingto do with security. They are generally intended to enforce data/code/object integrity.

附注:在publicprivateprotected关键字都几乎没有什么做的安全性。它们通常用于强制执行数据/代码/对象完整性。