Function call in class method?

I tried to figure out how to do this, but I'm not quite sure how to do this.

Here is an example of what I'm 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'm having problems with, how can I call bigTest ()?

+80
function methods php class call
Nov 12 '09 at 20:32
source share
10 answers

Try the following:

 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(); 
+167
Nov 12 '09 at 20:38
source share

The sample you provided is invalid PHP and has several problems:

 public scoreTest() { ... } 

is not a function declaration - you need to declare a function with the keyword "function".

The syntax should be sooner:

 public function scoreTest() { ... } 

Secondly, wrapping the bigTest () and smallTest () functions in a public function () {} does not make them private - you should use a private keyword for both of them separately:

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

It is also an agreement to capitalize class names in class declarations ("Test").

Hope this helps.

+14
Nov 12 '09 at 20:48
source share

I think you are looking for something like this.

 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(); 
+6
Sep 06 '15 at 3:54 on
source share

To call any method of an object created by an instance of a class (with the new instruction), you need to "point" to it. From the outside, you simply use the resource created by the new operator. Inside any PHP object created by the new one, the same resource is stored in the $ this variable. So, inside the class, you SHOULD point to the $ this method. In your class, to call smallTest from within the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:

 $this->smallTest(); 
+4
Dec 24 '14 at 16:58
source share

You need to call newTest so that the functions declared inside this method are "visible" (see Functions inside Functions ). But these are just ordinary functions and methods.

+3
Nov 12 '09 at 20:35
source share

To have a “function inside a function”, if I understand what you are asking for, you need PHP 5.3, where you can use the new Closure function.

So you could:

 public function newTest() { $bigTest = function() { //Big Test Here } } 
+3
Nov 12 '09 at 20:39
source share
  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

+2
Dec 10 '16 at 13:25
source share

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

+1
Aug 23 '16 at 17:25
source share

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') 
0
Oct 07 '13 at 19:20
source share
 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; } } 
0
Jul 13 '17 at 11:21 on
source share



All Articles