dtq81142 2013-10-01 21:44
浏览 45
已采纳

与以下代码有点混淆

I'm attempting to learn codeigniter for php and I've come across this block of code where it seems the instructor is trying to change a constant variable. First I'm clueless as to why the curly braces are used and then I'm curious as to exactly what is going on with that constant variable.

<?php
    class MY_Model extends CI_Model {
        const DB_TABLE = 'abstract';
        const DB_TABLE_PK = 'abstract';

        private function insert() {
            $this->db->insert($this::DB_TABLE, $this);
            $this->{$this::DB_TABLE_PK} = $this->db->insert_id();
        }
    }
?>

Can someone please explain not only the use of curly braces here, but also how it's possible to assign a new value to the defined constant?

  • 写回答

1条回答 默认 最新

  • doulu8415 2013-10-01 21:53
    关注

    They're not assigning to a constant. They're using the constant as a lookup key, to create a "dynamic" attribute of the object.

    This line:

    $this->{$this::DB_TABLE_PK} = $this->db->insert_id();
    

    boils down to

    $this->{'abstract'} = $this->db->insert_id();
    

    which is a perfectly acceptable construct. It's just creating an object attribute on-the-fly.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度