简体   繁体   English

PHP如何在类内部使用$ this-> app?

[英]PHP How can I able to use $this->app just inside class?

I am learning PHP these days. 这些天我正在学习PHP。 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); 函数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. 我如何在子类“ B”中使用$this ,我知道我可以在某些函数中使用$ this,但是我的意图是在该php文件中的任何地方都使用$ app,所以我试图在类内部创建$ app,因此我可以在任何地方使用$ app变量,否则我将在某些函数中使用$ this。

Here is my query: How can I able to use $this->app just inside class? 这是我的查询:如何在类内部使用$this->app


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) 获取错误为: PHP解析错误:语法错误,意外的'B'(T_STRING),期望变量(T_VARIABLE)

You are extending class A, so you receive all public / protected properties of this parent. 您正在扩展类A,因此您会收到此父级的所有公共/受保护的属性。 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. class属性的默认值必须是文字,它不能引用任何变量或调用函数。 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). 请注意,您不能仅通过$app$cfg来访问类属性,它们始终必须使用$this->进行限定(在引用类内部当前对象的属性时)。

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. B声明$app并没有多大意义,因为它会自动从A继承它,因此您可以将其作为$this->app来访问,而无需进行其他声明。

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. 您必须声明$ app在A中受保护,然后它将对类和所有子类可见,但在类外部不可见。

class A {
    protected $app;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM