Access global variable from within a class

前端 未结 4 1009
轮回少年
轮回少年 2020-12-04 02:00

I have the following (stripped down) code:



        
4条回答
  •  醉梦人生
    2020-12-04 02:24

    please don't use the global method that is being suggested. That makes my stomach hurt.

    Pass $a into the constructor of B.

    class A {
        function Show(){
                echo "ciao";
        }
    }
    
    $a = new A();
    $b = new B( $a );
    
    class B {
        function __construct( $a ) {
            $a->Show();
        }
    }
    

提交回复
热议问题