116

我一直在试图弄清楚如何去做,但我不太确定如何去做。

这是我正在尝试做的一个例子:

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

这是我遇到问题的部分,如何调用 bigTest()?

4

10 回答 10

211

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();
于 2009-11-12T20:38:36.203 回答
24

您提供的示例不是有效的 PHP,并且存在一些问题:

public scoreTest() {
    ...
}

不是正确的函数声明——您需要使用 'function' 关键字声明函数。

语法应该是:

public function scoreTest() {
    ...
}

其次,将 bigTest() 和 smallTest() 函数包装在 public function() {} 中不会使它们成为私有的——您应该分别对这两个函数使用 private 关键字:

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

此外,在类声明('Test')中将类名大写是惯例。

希望有帮助。

于 2009-11-12T20:48:19.193 回答
15
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;
    }
 }
于 2017-07-13T11:21:16.423 回答
13

我认为您正在寻找类似的东西。

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();
于 2015-09-06T03:54:37.467 回答
5

要调用从类实例化的对象的任何方法(使用语句 new),您需要“指向”它。从外部您只需使用由新语句创建的资源。在 new 创建的任何对象 PHP 中,将相同的资源保存到 $this 变量中。所以,在一个类中,你必须通过 $this 指向方法。在您的类中,smallTest要从类内部调用,您必须告诉 PHP 您要执行由 new 语句创建的所有对象中的哪一个,只需编写:

$this->smallTest();
于 2014-12-24T16:58:41.590 回答
4

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
   }
}
于 2009-11-12T20:39:24.080 回答
4
  class sampleClass
    { 
        public function f1()
        {
           return "f1 run";
        }

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

    f2();  

    }

输出 :

f2 运行 f1 运行

于 2016-12-10T13:25:03.173 回答
3

您需要调用newTest以使在该方法中声明的函数“可见”(请参阅​​函数中的函数)。但这只是正常的功能,没有方法。

于 2009-11-12T20:35:11.857 回答
2

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

示例2

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')
于 2013-10-07T19:20:30.580 回答
2

如果要调用当前类的静态变量或函数,也可以使用self::CONST代替。$this->CONST

于 2016-08-23T17:25:09.727 回答