Calling a function within a Class method?

410,403

Solution 1

Try this one:

class test {
     public function newTest(){
          $this->bigTest();
          $this->smallTest();
     }

     private function bigTest(){
          //Big Test Here
     }

     private function smallTest(){
          //Small Test Here
     }

     public function scoreTest(){
          //Scoring code here;
     }
}

$testObject = new test();

$testObject->newTest();

$testObject->scoreTest();

Solution 2

The sample you provided is not valid PHP and has a few issues:

public scoreTest() {
    ...
}

is not a proper function declaration -- you need to declare functions with the 'function' keyword.

The syntax should rather be:

public function scoreTest() {
    ...
}

Second, wrapping the bigTest() and smallTest() functions in public function() {} does not make them private — you should use the private keyword on both of these individually:

class test () {
    public function newTest(){
        $this->bigTest();
        $this->smallTest();
    }

    private function bigTest(){
        //Big Test Here
    }

    private function smallTest(){
           //Small Test Here
    }

    public function scoreTest(){
      //Scoring code here;
    }
}

Also, it is convention to capitalize class names in class declarations ('Test').

Hope that helps.

Solution 3

class test {
    public newTest(){
        $this->bigTest();
        $this->smallTest();
    }

    private  function bigTest(){
        //Big Test Here
    }

    private function smallTest(){
       //Small Test Here
    }

    public scoreTest(){
      //Scoring code here;
    }
 }

Solution 4

I think you are searching for something like this one.

class test {

    private $str = NULL;

    public function newTest(){

        $this->str .= 'function "newTest" called, ';
        return $this;
    }
    public function bigTest(){

        return $this->str . ' function "bigTest" called,';
    }
    public function smallTest(){

        return $this->str . ' function "smallTest" called,';
    }
    public function scoreTest(){

        return $this->str . ' function "scoreTest" called,';
    }
}

$test = new test;

echo $test->newTest()->bigTest();

Solution 5

To call any method of an object instantiated from a class (with statement new), you need to "point" to it. From the outside you just use the resource created by the new statement. Inside any object PHP created by new, saves the same resource into the $this variable. So, inside a class you MUST point to the method by $this. In your class, to call smallTest from inside the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:

$this->smallTest();
Share:
410,403

Related videos on Youtube

WAC0020
Author by

WAC0020

Updated on July 08, 2022

Comments

  • WAC0020
    WAC0020 almost 2 years

    I have been trying to figure out how to go about doing this but I am not quite sure how.

    Here is an example of what I am trying to do:

    class test {
         public newTest(){
              function bigTest(){
                   //Big Test Here
              }
              function smallTest(){
                   //Small Test Here
              }
         }
         public scoreTest(){
              //Scoring code here;
         }
    }
    

    Here is the part I am having problems with, how do I call bigTest()?

    • markus
      markus over 14 years
      Just to make sure: a function and a method is exactly the same function === method. The term method is more often used in OO language to describe the function of a class.
    • WAC0020
      WAC0020 over 14 years
      The reason some of the terms are missing is I was on my way out of the office, so I was short on time.
    • 99electronic
      99electronic over 2 years
      if what you need is to call bigTest fn from within scoreTest, you have to call it $this->bigTest();
  • tfont
    tfont over 10 years
    $rentry->newTest()->bigTest(); $rentry->newTest()->smallTest(); ---- Fatal error: Cannot redeclare bigTest() (previously declared.
  • James111
    James111 almost 9 years
    Is it possible to run a function() from another .php page inside a class function and then grab results inside the class function? e.g I have a query that selects all from a table and then returns a fetch all result set. Is it possible to loop through that result set inside a classes function? e.g class query{ public function show(){ getResults(); while($stmt->fetchCollumn()){ ECHO RESULTS HERE }
  • Admin
    Admin almost 4 years
    Fatal error: Using $this when not in object context