dsc71976 2014-12-03 08:51
浏览 244
已采纳

将$ this变量作为参数传递

I need help in understanding the following php code.

    $this->_PageHeader = new PAGE_HEADER($this);

I want to understand the functionality of $this argument in PAGE_HEADER($this).

I knew that $this is used inside a class to refer property and methods but in this case, what values will be passed through this argument.

  • 写回答

1条回答 默认 最新

  • douye2110 2014-12-03 09:17
    关注

    $this contains the current instance of the class you are in. So it means that you pass the current object of the class containing the code $this->_PageHeader = new PAGE_HEADER($this); to the constructor of the class PAGE_HEADER.

    For instance:

    class A 
    {
        public $value = 1;
    
        public function execute()
        {
            $b = new B($this);
        }
    }
    
    class B
    {
        public $value = 2;
    
        public function __construct(A $dep)
        {
            echo $dep->value; // 3
            echo $this->value; // 2
        }
    }
    
    $a = new A();
    $a->value = 3;
    $a->execute(); // In this execution, $this is the object $a.
    

    This code will output 32.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案