这是我的第一个面向对象编程程序,请不要生气 :)
问题在于我遇到了以下错误:
该程序简单地对字符串进行编码和解码。我不确定这是否是学习面向对象编程的好例子。无法访问 D:\xampp\htdocs\php\OOP\coder_class.php 第47行中的 Code::$text 受保护属性
<?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();
能有人帮我修复这个问题吗?我是PHP新手。
public function getText() { return $this->text; }
然后echo $text->getText();
- Waygood$this->text
而不是parent::text
。子类可以访问父类的受保护属性。 - akDeveloper