我可以使用变量而不用PHP中的$ this-> myvar来声明吗?

我可以使用变量而不用PHP中的$ this-> myvar来声明吗?

问题描述:

In the following code, variables user and permissions are not declared at the beginning like $data, $module etc. And these $this->user and $this->permissions are used in the extended class of this class.

My question is can I use variables without declaring and using $this->myvar?

Thanks in advance.

class MY_Controller extends CI_Controller {

// Deprecated: No longer used globally
protected $data;
public $module;
public $controller;
public $method;

public function MY_Controller()
{
      .......
     $this->user = $this->ion_auth->get_user();
     .........
     // List available module permissions for this user
    $this->permissions = $this->user ? 
     $this->permission_m->get_group($this->user->group_id) : array();

在下面的代码中,变量user和permissions在开头没有声明,如$ data,$ module等。 这个$ this-> user和$ this->权限用在这个类的扩展类中。 p>

我的问题是我可以使用变量而不声明并使用$ this-&gt ; myvar? p>

提前致谢。 p>

 类MY_Controller扩展CI_Controller {
 
 //弃用:不再全局使用\  nprotected $ data; 
public $ module; 
public $ controller; 
public $ method; 
 
public function MY_Controller()
 {
 ....... 
 $ this-> user = $ this  - > ion_auth-> get_user(); 
 ......... 
 //列出此用户的可用模块权限
 $ this-> permissions = $ this-> user?  
 $ this-> permission_m-> get_group($ this-> user-> group_id):array(); 
  code>  pre> 
  div>

Yes you can.

PHP automatically creates public properties when initially set, eg

class MyClass
{
    private $foo;

    public function __construct()
    {
        $this->bar = 'bar'; // Now contains "public $bar"
    }
}

The only time this differs is if you have a magic __set($name, $value) method which will catch an attempt to write to an undefined or non-visible (in the current scope) property.

Yes !

Maybe this send a 'warning message', you can use ini_set('display_errors', 0) to avoid it.