BMLG Learning
BMLG Learning

Reputation: 59

Fatal error with function

When I create the object SearchResult, I have the constructor call 'setStandardsTable' function that sets variable 'standardsTable'. For some reason I get the error

Fatal error: Uncaught Error: Call to undefined function setStandardsTable()...

and

Error: Call to undefined function setStandardsTable()...

I tried returning the value after each variable declaration but still nothing. Below is my code.

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;
    }
}

Upvotes: 0

Views: 146

Answers (4)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21681

As others mentions you have to call it with $this

$this->standardsTable = $this->setStandardsTable($subject); 

That said something others failed to mention that i think would be of great benifit would be to instead use a factory method. So instead of one overwhelming class you would make many.

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;
    }
}

Now the really big advantage here is that these look like they would include the lesser classes functionallity. So for example AlgebraII could include stuff from Algebra which could include stuff from Math.

So when you add these other classes in:

class AlgebraResults extends MathResults{
    public function getTable(){
        return 'algebra_standards_eng';
    }
}

class AlgebraIIResults extends AlgebraResults{
    public function getTable(){
        return 'algebra_two_standards_eng';
    }
}

Or you could just extend AbstractSearchResult instead of the level above it, as I have done.

So rather or not this makes sense in your use case, I don't know. But if you have a bunch of functionality you need to impliment, it may be much more streamlined to break this up some.

That said, don't break them up for the sake of breaking them up. If the functionality is generally the same and the code-base is small, then there probably isn't a need. But if you find yourself coding things wildly different for Algebra and Math, then it might make sense to separate them.

The main advantage you git from this, is smaller more focused classes/files (each class should be in its own file). Later it will be easier to add stuff, like if you add Calculus then you just add a new class in and don't have to edit the existing code much. etc.

Hope that makes sense.

Upvotes: 0

Sunil Nagre
Sunil Nagre

Reputation: 9

You can not call directly a member function of Class, you should specify object to call member function. Simply you can using $this->setStandardsTable($subject)

Upvotes: 0

bytecode77
bytecode77

Reputation: 14880

In PHP, you need to use self:: and $this-> to call functions within the same class:

Call to a static function:

$variable = self::SomeStaticFunction();

Call to a non-static function:

$variable = $this->FunctionThatIsntStatic();

...instead of just the function name, which is expected to be in the global namespace.

$this->standardsTable = SomeFunction();

In your case: It's $this->setStandardsTable($subject);

Upvotes: 2

Vaibhav Gehlot
Vaibhav Gehlot

Reputation: 60

It seems that you just have to do a minor change just replace

$this->standardsTable = setStandardsTable($subject);

With

$this->standardsTable = $this->setStandardsTable($subject); 

Upvotes: 0

Related Questions