使用其他类的PHP类变量

时间:2012-01-06 04:23:21

标签: php class variables

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

这是函数

    public function __construct($id = self::DHMeetingCommentCardNew, DHMeetingGroup $meetingGroup = null)
    {
        global $DB;

        if ($id == self::DHMeetingCommentCardNew)
        {
            var_dump($meetingGroup);
            $query = "INSERT INTO `" . self::DHMeetingCommentCardTable . "` (`UniqueID`, `MeetingGroup`) VALUES (UUID(), '{$meetingGroup['_id']}')";
            $DB->query($query);

            $this->_id = $DB->lastID();
            $this->_initializeNewCommentCard();
        }
        else
        {
            $this->_id = intval($id);
        }
    }

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

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

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

2 个答案:

答案 0 :(得分:0)

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

您唯一能做的就是更改_id中从DHMeetingGroupprivate

public属性的声明方式

答案 1 :(得分:0)

尝试将此代码添加到您的班级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()}')";