Calling a function within a Class method?

PhpFunctionClassMethodsCall

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

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 - Php

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 - Php

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

Solution 6 - Php

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

Solution 7 - Php

  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

Solution 8 - Php

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.

Solution 9 - Php

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')

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWAC0020View Question on Stackoverflow
Solution 1 - PhpSergey KuznetsovView Answer on Stackoverflow
Solution 2 - PhppjbeardsleyView Answer on Stackoverflow
Solution 3 - PhpMeganathan SView Answer on Stackoverflow
Solution 4 - PhpAli HasanView Answer on Stackoverflow
Solution 5 - PhpIngenieroView Answer on Stackoverflow
Solution 6 - PhpblockheadView Answer on Stackoverflow
Solution 7 - PhpMasoud SiahkaliView Answer on Stackoverflow
Solution 8 - PhpGumboView Answer on Stackoverflow
Solution 9 - PhpzloctbView Answer on Stackoverflow
Solution 10 - PhpAlexioVayView Answer on Stackoverflow