【问题标题】:Using PHP Class Variables from other class使用其他类的 PHP 类变量
【发布时间】:2012-02-03 22:00:27
【问题描述】:

我遇到了一组代码的问题。我无法让变量显示。

这里是函数

公共函数 __construct($id = self::DHMeetingCommentCardNew, DHMeetingGroup $meetingGroup = null) { 全球$DB; if ($id == self::DHMeetingCommentCardNew) { var_dump($meetingGroup); $query = "INSERT INTO `" 。自我::DHMeetingCommentCardTable 。 "` (`UniqueID`, `MeetingGroup`) 值 (UUID(), '{$meetingGroup['_id']}')"; $DB->查询($查询); $this->_id = $DB->lastID(); $this->_initializeNewCommentCard(); } 别的 { $this->_id = intval($id); } }

我无法从 $meetingGroup 变量中获取 ID。

对于 var_dump($meetingGroup),它会转储这个。

NULL 对象(DHMeetingGroup)#10745 (1) { ["_id":"DHMeetingGroup":private]=> int(11086) }

【问题讨论】:

    标签: php class variables


    【解决方案1】:

    只要属性定义为property,您就只能从同一个实例(和同一个类)访问它,而不能从外部访问它。

    您唯一能做的就是将DHMeetingGroup_id 属性的声明方式从private 更改为public

    【讨论】:

    • 我将定义从私有更改为公有,现在 var_dump 将其输出。
    • @user764429:我不明白你的意思
    【解决方案2】:

    尝试将此代码添加到您的班级 DHMeetingGroup

    public function get_id()
    {
        return $this->_id;
    }
    

    然后更改这一行(在我猜的类中称为 DHMeetingCommentCard)

    $query = "INSERT INTO `" . self::DHMeetingCommentCardTable . "` (`UniqueID`, `MeetingGroup`) VALUES (UUID(), '{$meetingGroup['_id']}')";
    

    $query = "INSERT INTO `" . self::DHMeetingCommentCardTable . "` (`UniqueID`, `MeetingGroup`) VALUES (UUID(), '{$meetingGroup->get_id()}')";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多