Php call method in class

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()?

tshepang

11.7k21 gold badges90 silver badges134 bronze badges

asked Nov 12, 2009 at 20:32

3

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();

answered Nov 12, 2009 at 20:38

Sergey KuznetsovSergey Kuznetsov

8,4614 gold badges24 silver badges21 bronze badges

1

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.

answered Nov 12, 2009 at 20:48

pjbeardsleypjbeardsley

1,4411 gold badge13 silver badges16 bronze badges

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

answered Jul 13, 2017 at 11:21

Php call method in class

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();

Php call method in class

bittusarkar

6,1273 gold badges28 silver badges50 bronze badges

answered Sep 6, 2015 at 3:54

Php call method in class

Ali HasanAli Hasan

1762 silver badges10 bronze badges

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();

Php call method in class

Makyen

30.5k12 gold badges78 silver badges117 bronze badges

answered Dec 24, 2014 at 16:58

IngenieroIngeniero

511 silver badge2 bronze badges

1

In order to have a "function within a function", if I understand what you're asking, you need PHP 5.3, where you can take advantage of the new Closure feature.

So you could have:

public function newTest() {
   $bigTest = function() {
        //Big Test Here
   }
}

answered Nov 12, 2009 at 20:39

blockheadblockhead

9,5573 gold badges42 silver badges68 bronze badges

  class sampleClass
    { 
        public function f1()
        {
           return "f1 run";
        }

        public function f2()
        {
           echo ("f2 run" );
           $result =  $this->f1();
           echo ($result);
        }   

    f2();  

    }

output :

f2 run f1 run

answered Dec 10, 2016 at 13:25

Php call method in class

Masoud SiahkaliMasoud Siahkali

4,7151 gold badge27 silver badges18 bronze badges

You need to call newTest to make the functions declared inside that method “visible” (see Functions within functions). But that are then just normal functions and no methods.

answered Nov 12, 2009 at 20:35

GumboGumbo

628k106 gold badges767 silver badges838 bronze badges

example 1

class TestClass{
public function __call($name,$arg){
call_user_func($name,$arg);
}
}
class test {
     public function newTest(){

          function bigTest(){
               echo 'Big Test Here';
          }
          function smallTest(){
               echo 'Small Test Here';
          }

$obj=new TestClass;

return $obj;
     }

}
$rentry=new test;
$rentry->newTest()->bigTest();

example2

class test {
     public function newTest($method_name){

          function bigTest(){
               echo 'Big Test Here';
          }
          function smallTest(){
               echo 'Small Test Here';
          }

      if(function_exists( $method_name)){    
call_user_func($method_name);
      }
      else{
          echo 'method not exists';
      }
     }

}
$obj=new test;
$obj->newTest('bigTest')

answered Oct 7, 2013 at 19:20

zloctbzloctb

9,8906 gold badges67 silver badges85 bronze badges

1

You can also use self::CONST instead of $this->CONST if you want to call a static variable or function of the current class.

answered Aug 23, 2016 at 17:25

Php call method in class

AlexioVayAlexioVay

3,8722 gold badges28 silver badges45 bronze badges

How do you call a method inside a class in PHP?

How to call a method?.
First, we create an object ( $example ) from the class Example..
Next, we call the method echo with -> (object operator) and () (parentheses).
The parentheses contain the arguments as usual..

What is __ call () in PHP?

When you call a method on an object of the Str class and that method doesn't exist e.g., length() , PHP will invoke the __call() method. The __call() method will raise a BadMethodCallException if the method is not supported. Otherwise, it'll add the string to the argument list before calling the corresponding function.

How do you call a method in PHP?

To invoke a method on an object, you simply call the object name followed by "->" and then call the method. Since it's a statement, you close it with a semicolon. When you are dealing with objects in PHP, the "->" is almost always used to access that object, whether it's a property or to call a method.

How do you call a method inside a function in PHP?

php class class_name { function b() { echo 'test'; } function c() { // This function belongs inside method "c". It accepts a single parameter which is meant to be an instance of "class_name". function a($that) { $that->b(); } // Call the "a" function and pass an instance of "$this" by reference.