Php 函数的致命错误
当我创建对象搜索结果时,构造函数调用了设置变量“standardsTable”的“setStandardsTable”函数。由于某种原因,我得到了这个错误 致命错误:未捕获错误:调用未定义函数 setStandardsTable() 及 错误:调用未定义的函数setStandardsTable() 我尝试在每个变量声明后返回值,但仍然没有返回任何值。下面是我的代码Php 函数的致命错误,php,function,class,Php,Function,Class,当我创建对象搜索结果时,构造函数调用了设置变量“standardsTable”的“setStandardsTable”函数。由于某种原因,我得到了这个错误 致命错误:未捕获错误:调用未定义函数 setStandardsTable() 及 错误:调用未定义的函数setStandardsTable() 我尝试在每个变量声明后返回值,但仍然没有返回任何值。下面是我的代码 class SearchResult { private $keyword, $standardsTable;
class SearchResult {
private $keyword, $standardsTable;
/*Constructor */
public function __construct($subject, $keyword) {
//selects the standards table to query based on subject selected
$this->standardsTable = setStandardsTable($subject);
//sets the keyword that will be used to search in the Standards table selected
$this->keyword = $keyword;
}
private function setStandardsTable($subj) {
$standardsSelected="";
switch($subj) {
case "General Math":
$standardsSelected = "math_standards_eng";
break;
case "Algebra I":
$standardsSelected ="algebra_standards_eng";
break;
case "Algebra II":
$standardsSelected = "algebra_two_standards_eng";
break;
default:
$standardsSelected = "math_standards_eng";
}
return $standardsSelected;
}
}
在PHP中,需要使用
self::
和$this->
调用同一类中的函数:
调用静态函数:
$variable = self::SomeStaticFunction();
$variable = $this->FunctionThatIsntStatic();
对非静态函数的调用:
$variable = self::SomeStaticFunction();
$variable = $this->FunctionThatIsntStatic();
…而不仅仅是函数名,函数名应位于全局命名空间中
$this->standardsTable = SomeFunction();
在您的情况下:它是
$this->setStandardsTable($subject)
在PHP中,您需要使用self::
和$this->
调用同一类中的函数:
调用静态函数:
$variable = self::SomeStaticFunction();
$variable = $this->FunctionThatIsntStatic();
对非静态函数的调用:
$variable = self::SomeStaticFunction();
$variable = $this->FunctionThatIsntStatic();
…而不仅仅是函数名,函数名应位于全局命名空间中
$this->standardsTable = SomeFunction();
在您的情况下:它是
$this->setStandardsTable($subject)代码>看起来您只需做一个小的更改,就可以替换
$this->standardsTable = setStandardsTable($subject);
与
看来你只需要做一点小小的改变就可以了
$this->standardsTable = setStandardsTable($subject);
与
不能直接调用类的成员函数,应指定调用成员函数的对象。
只需使用$this->setStandardsTable($subject)您不能直接调用类的成员函数,您应该指定对象来调用成员函数。
您可以简单地使用$this->setStandardsTable($subject)正如其他人提到的,您必须使用$this
$this->standardsTable = $this->setStandardsTable($subject);
也就是说,其他人没有提到,我认为使用工厂方法会有很大的好处。因此,你可以做很多,而不是一门势不可挡的课
interface SearchResultInterface{
public function setKeyword($keyword);
public function getKeyword();
public function getTable();
}
abstract AbstractSearchResult impliments SearchResultInterface{
protected $keyword;
public function setKeyword($keyword)
{
$this->keyword = $keyword;
}
public function getKeyword(){
return $this->keyword;
}
abstract function getTable();
}
class MathResults extends AbstractSearchResult{
public function getTable(){
return 'math_standards_eng';
}
}
class SearchResultFactory {
static function createResult($subj, $keyword) {
switch($subj) {
case "Algebra I":
$Result = new AlgebraResults();
break;
case "Algebra II":
$Result = new AlgebraIIResults();
break;
case "General Math":
default:
$Result = new MathResults();
}
$Result->setKeyword( $keyword );
return $Result;
}
}
现在这里最大的优势是,它们看起来像是包含了较小的类功能性。例如,代数可以包括代数中的东西,也可以包括数学中的东西
因此,当您在中添加这些其他类时:
class AlgebraResults extends MathResults{
public function getTable(){
return 'algebra_standards_eng';
}
}
class AlgebraIIResults extends AlgebraResults{
public function getTable(){
return 'algebra_two_standards_eng';
}
}
或者您可以扩展AbstractSearchResult
,而不是像我所做的那样,扩展它上面的级别
所以,我不知道这在您的用例中是否有意义。但是,如果您有一系列需要实现的功能,那么分解这些功能可能会更加精简
也就是说,不要为了拆散他们而拆散他们。如果功能基本相同且代码库较小,则可能不需要。但如果你发现自己为代数和数学编写的代码大不相同,那么将它们分开可能是有意义的
您从中获得的主要优势是更小、更集中的类/文件(每个类都应该在自己的文件中)。以后添加东西会更容易,比如如果您添加微积分,那么您只需添加一个新类,而不必对现有代码进行太多编辑。等等
希望这是有道理的 正如其他人提到的,您必须用$this
$this->standardsTable = $this->setStandardsTable($subject);
也就是说,其他人没有提到,我认为使用工厂方法会有很大的好处。因此,你可以做很多,而不是一门势不可挡的课
interface SearchResultInterface{
public function setKeyword($keyword);
public function getKeyword();
public function getTable();
}
abstract AbstractSearchResult impliments SearchResultInterface{
protected $keyword;
public function setKeyword($keyword)
{
$this->keyword = $keyword;
}
public function getKeyword(){
return $this->keyword;
}
abstract function getTable();
}
class MathResults extends AbstractSearchResult{
public function getTable(){
return 'math_standards_eng';
}
}
class SearchResultFactory {
static function createResult($subj, $keyword) {
switch($subj) {
case "Algebra I":
$Result = new AlgebraResults();
break;
case "Algebra II":
$Result = new AlgebraIIResults();
break;
case "General Math":
default:
$Result = new MathResults();
}
$Result->setKeyword( $keyword );
return $Result;
}
}
现在这里最大的优势是,它们看起来像是包含了较小的类功能性。例如,代数可以包括代数中的东西,也可以包括数学中的东西
因此,当您在中添加这些其他类时:
class AlgebraResults extends MathResults{
public function getTable(){
return 'algebra_standards_eng';
}
}
class AlgebraIIResults extends AlgebraResults{
public function getTable(){
return 'algebra_two_standards_eng';
}
}
或者您可以扩展AbstractSearchResult
,而不是像我所做的那样,扩展它上面的级别
所以,我不知道这在您的用例中是否有意义。但是,如果您有一系列需要实现的功能,那么分解这些功能可能会更加精简
也就是说,不要为了拆散他们而拆散他们。如果功能基本相同且代码库较小,则可能不需要。但如果你发现自己为代数和数学编写的代码大不相同,那么将它们分开可能是有意义的
您从中获得的主要优势是更小、更集中的类/文件(每个类都应该在自己的文件中)。以后添加东西会更容易,比如如果您添加微积分,那么您只需添加一个新类,而不必对现有代码进行太多编辑。等等
希望这是有道理的 谢谢大家!!我不知道这一点,但从现在起我会尽量记住这一点谢谢你!我不知道这一点,但从现在起我会尽量记住这一点DSo整个类作为一个整体会被视为工厂方法吗?那么整个类作为一个整体会被视为工厂方法吗?