PHP中的成员和函数继承
我还是PHP新手,遇到了很多麻烦。我习惯了C语言、C++语言和java语言,这一点让我感到困惑。基本上我的问题是我有以下代码:PHP中的成员和函数继承,php,oop,Php,Oop,我还是PHP新手,遇到了很多麻烦。我习惯了C语言、C++语言和java语言,这一点让我感到困惑。基本上我的问题是我有以下代码: class File_Reader { protected $text; public function Scan_File (){} public function Skip_Whitespace(&$current_pos) { //skip past whitespace at the start
class File_Reader
{
protected $text;
public function Scan_File (){}
public function Skip_Whitespace(&$current_pos)
{
//skip past whitespace at the start
while (($current_pos < strlen($text) && ($text[$current_pos] == ' ')))
$current_pos++;
}
public function __construct(&$file_text)
{
$text = $file_text;
}
}
class Times_File_Reader extends File_Reader
{
Public Function Scan_File()
{
$errors = array();
$times = array();
$current_time;
$cursor = 0;
$line = 0;
while ($cursor < strlen($text))
{
Skip_Whitespace($cursor);
//....lots more code here...
return $times;
}
}
}
我已经寻找了几个小时的答案,但毫无结果,最后期限很快就要到了。任何帮助都将不胜感激。谢谢大家! 您需要将传递到构造函数中的属性设置为类的属性(方法中的变量的作用域与Java相同) 您可以使用$this->property执行此操作
// File_Reader
public function __construct(&$file_text)
{
$this->text = $file_text;
}
// Times_File_Reader
public function Scan_File()
{
$errors = array();
$times = array();
$current_time;
$cursor = 0;
$line = 0;
while ($cursor < strlen($this->text))
{
$this->Skip_Whitespace($cursor);
//....lots more code here...
return $times;
}
}
另外-您通过引用(&$var)传递变量-您可能不必这样做(我能想到的唯一有效用例是在某些情况下使用闭包/匿名函数)。我承认这方面的文件不是很清楚
我相信您需要注意,您是通过使用
$this->跳过空白($cursor)代码>它不仅仅是$this->跳过空白($cursor)
您还需要使用$this->text
,正如下一个答案所建议的那样。@PhillSparks您是对的。我被他提到的$time弄糊涂了,我想他指的是$text。好的,这很有帮助。非常感谢。但是,我仍然不能使用Skip_空格($cursor);它给我的错误是:致命错误:调用C:\xampp\htdocs\Software Engineering\File\u Readers.php中的未定义函数Skip_Whitespace(),第58行,有任何关于该部分不起作用的线索吗?同样,您需要使用$this->method()。相应地更新了代码。确定。非常感谢。此外,就“古怪的下划线酶/标题酶杂交”而言,这不是我的选择。队长出于某种原因不喜欢驼峰案,并拒绝让它成为我们编码标准的一部分:\
// File_Reader
public function __construct(&$file_text)
{
$this->text = $file_text;
}
// Times_File_Reader
public function Scan_File()
{
$errors = array();
$times = array();
$current_time;
$cursor = 0;
$line = 0;
while ($cursor < strlen($this->text))
{
$this->Skip_Whitespace($cursor);
//....lots more code here...
return $times;
}
}
class FileReader
class TimesFileReader
public function scanFile()
public function __construct($file_text)
{
$this->text = $file_text;
}