PHP如何才能在课堂内使用$ this-> app?

I am learning PHP these days. Sorry, if my below query look dumb.

 <?php
class A
{
   var $parent;
   var $app;

   function A($parent) {

      $this->parent = $parent;

      if ($parent->isApplication()) {
         $this->app = $parent;
      } else {
         $this->app = $parent->getApplication();
      }
   }


}
?>



<?php
class B extends A

{
    private $app;
    private $cfg;

    public B() {
        $this->app = parent::$app;
        $this->cfg = $this->app->cfg;
    }


}
?>


<?php

class C extends A {

function x(){ new B($this); // calling somewhere in some
} }

?>

How do I use $this inside subclass 'B', I knew that I can use $this inside some function, BUT my intention is to use $app everywhere in that php file, so I am trying to create $app just inside class so that I can use this $app variable anywhere, otherwise I would have used $this inside some function.

Here is my query: How can I able to use $this->app just inside class?


UPDATE:

  <?php
class A
{
   var $parent;
   var $app;

   function Canvas($parent) {

      $this->parent = $parent;

      if ($parent->isApplication()) {
         $this->app = $parent;
      } else {
         $this->app = $parent->getApplication();
      }
   }


}
?>


<?php
class B extends A

{
    private $app;
    private $cfg;

    public Canvas_Access_Approval() {
        $this->app = parent::$app;
        $this->cfg = $this->app->cfg;
    }


}
?>


<?php

class C extends A {

    function x(){
       new B($this);
    }
}

?>

Getting Error as: PHP Parse error: syntax error, unexpected 'B' (T_STRING), expecting variable (T_VARIABLE)

You are extending class A, so you receive all public / protected properties of this parent. It means:

class A {
    public $app = 'test';
}

class B extends A {
    public function get() {
        echo $this->app;
    }
}

new B; // test

or (but unnecessarily):

class B extends A {
    private $bApp;

    public function __construct() {
        echo $this->bApp = $this->app;
    }
}

The default value for a class property has to be a literal, it can't refer to any variables or call functions. If you want to compute the default value of a property, you have to do it in the constructor method:

class B extends A {
    private $app;
    private $cfg;

    public function __construct() {
        $this->app = parent::$app;
        $this->cfg = $this->app->cfg;
    }
}

Note that you can't access class properties as simply $app or $cfg, they always have to be qualified with $this-> (when referring to properties of the current object inside the class).

There's not really much point to declaring $app in B, since it automatically inherits it from A, so you can just access it as $this->app without the additional declaration.

You have to declare $app protected in A, then it will be visible to the class and all subclasses, but not outside of the class.

class A {
    protected $app;
}

class B extends A {
    public function demo() {
        $cfg = $this->app->config;
    }
}