Calling a function within a Class method?

前端 未结 10 1001
庸人自扰
庸人自扰 2020-12-02 05:14

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 {
          


        
相关标签:
10条回答
  • 2020-12-02 05:55

    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.

    0 讨论(0)
  • 2020-12-02 05:55

    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();
    
    0 讨论(0)
  • 2020-12-02 05:58
      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

    0 讨论(0)
  • 2020-12-02 06:00

    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();
    
    0 讨论(0)
  • 2020-12-02 06:03

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

    0 讨论(0)
  • 2020-12-02 06:06

    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.

    0 讨论(0)
提交回复
热议问题