【问题标题】:Calling a function within a Class method?在类方法中调用函数?
【发布时间】:2010-12-16 01:25:49
【问题描述】:

我一直在想办法做到这一点,但我不太确定该怎么做。

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

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

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

【问题讨论】:

  • 只是为了确保:一个函数和一个方法是完全相同的函数 === 方法。方法一词在 OO 语言中更常用于描述类的功能。
  • 缺少某些条款的原因是我正在离开办公室的路上,所以时间不够。
  • 如果你需要从 scoreTest 中调用 bigTest fn,你必须调用它 $this->bigTest();

标签: php function class methods call


【解决方案1】:
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;
    }
 }

【讨论】:

    【解决方案2】:
      class sampleClass
        { 
            public function f1()
            {
               return "f1 run";
            }
    
            public function f2()
            {
               echo ("f2 run" );
               $result =  $this->f1();
               echo ($result);
            }   
    
        f2();  
    
        }
    

    输出:

    f2 运行 f1 运行

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

          $this->smallTest();
          

          【讨论】:

          • 致命错误:不在对象上下文中使用 $this
          【解决方案6】:

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

          【讨论】:

          • $rentry->newTest()->bigTest(); $rentry->newTest()->smallTest(); ---- 致命错误:无法重新声明 bigTest()(之前声明过。
          【解决方案7】:

          您提供的示例不是有效的 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')中类名大写是惯例。

          希望对您有所帮助。

          【讨论】:

            【解决方案8】:

            为了拥有“函数中的函数”,如果我理解您的要求,您需要 PHP 5.3,您可以在其中利用新的 Closure 功能。

            所以你可以:

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

            【讨论】:

              【解决方案9】:

              试试这个:

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

              【讨论】:

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

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

              【讨论】:

                猜你喜欢
                • 2019-07-27
                • 1970-01-01
                • 2013-03-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-01-27
                • 1970-01-01
                相关资源
                最近更新 更多