How do I Instantiate a class within a class in .php

前端 未结 3 1400
花落未央
花落未央 2021-01-27 10:56

I\'m new to OOP and I can\'t figure out why this isn\'t working. Is it not ok to instantiate a class with in a class. I\'ve tried this with included file with in the method an

3条回答
  •  鱼传尺愫
    2021-01-27 11:00

    You can access your private method with the help of create another public function in the same class and call the private function in the public method or function. Now you can call your private function with the help of $run instance.

    For example:

    class Cars{

    function getting(){
        echo ("Hello World");   
    }
    
    private function getting2(){
        echo ("Hello World Againg");
    }
    
    public function getting3(){
        $this->getting2();
    }
    

    }

    $class_object=new Cars;

    print($class_object->getting3());


    Your output screen like the below image.


提交回复
热议问题