在类方法中调用函数?

120

我一直在试图弄清楚如何处理这个问题,但我不太确定该怎么做。

下面是我想要实现的一个例子:

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

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


2
只是为了确保:函数和方法是完全相同的函数 === 方法。术语方法更常用于面向对象语言中描述类的功能。 - markus
一些术语缺失的原因是我正在离开办公室,所以时间很紧。 - WAC0020
如果你需要在scoreTest函数内部调用bigTest函数,你必须使用$this->bigTest()进行调用。 - Paul
10个回答

218

试试这个:

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

1
在类函数内部从另一个.php页面运行function(),然后在类函数内获取结果是否可能?例如,我有一个查询,它从表中选择所有内容,然后返回一个获取所有结果集。是否可以在类函数内循环遍历该结果集?例如:class query{ public function show(){ getResults(); while($stmt->fetchCollumn()){ ECHO RESULTS HERE } - James111

27

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

public scoreTest() {
    ...
}

这不是一个正确的函数声明——你需要使用“function”关键字来声明函数。

语法应该改为:

public function scoreTest() {
    ...
}

其次,在public function() {}中包装bigTest()和smallTest()函数并不能使它们变成私有的 - 您应该在这两个函数上分别使用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')。

希望对你有所帮助。


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

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

5

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

$this->smallTest();

致命错误: 在非对象环境下使用$this - user8588978

4
为了实现“函数内嵌函数”,如果我理解你的问题正确,你需要使用PHP 5.3及以上版本,这个版本中你可以利用新的Closure功能。

因此,你可以这样写:

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

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

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

    f2();  

    }

输出:

f2 运行 f1 运行


3

您需要调用newTest来使在该方法内声明的函数“可见”(参见Functions within functions)。但这些函数只是普通的函数,而不是方法。


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

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

$rentry->newTest()->bigTest(); $rentry->newTest()->smallTest();致命错误:无法重新声明bigTest()(先前已声明)。 - tfont

2

如果您想调用当前类的静态变量或函数,可以使用self::CONST而不是$this->CONST


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接