douxie4583 2011-05-13 05:54
浏览 35
已采纳

我可以使用变量而不用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();
  • 写回答

2条回答 默认 最新

  • doutuoben6908 2011-05-13 05:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 UCOSⅢ,3.0.3升级为3.0.4后程序编译成功,但是运行后死在统计任务的地方
  • ¥15 python程序长时间运行卡死,付费求解决方案
  • ¥20 VM打开不了ubuntu虚拟机,如何解决?
  • ¥15 java请求一个返回流式数据的接口,如何将流式数据直接返回前端
  • ¥15 为什么连接不了啊,配置都没问题啊
  • ¥15 c语言做一个简单的计算器,大家来看看
  • ¥15 nuxtjs3+ts 报错,急呀!
  • ¥15 matlab矩阵复数本征值排序
  • ¥15 skynet MySQL ProtocolBuffers
  • ¥15 浏览器关闭事件有时没执行怎么回事