从 class - php 访问 属性

Accessing property from class - php

我目前正在使用 pimax 的 facebook 信使:https://github.com/pimax/fb-messenger-php

我想从 UserProfile.php 中的 class UserProfile 访问 first_name 字段并将其存储在一个变量中,以便我可以使用该名称进行个性化设置给用户的消息。我很难在不破坏机器人的情况下做到这一点,非常感谢您的帮助!

这是 UserProfile.php 文件:

<?php
namespace pimax;

class UserProfile
{
    protected $data = [];

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getFirstName()
    {
        return $this->data['first_name'];
    }

    public function getLastName()
    {
        return $this->data['last_name'];
    }

    public function getPicture()
    {
        return $this->data['profile_pic'];
    }
}

在此先感谢您的帮助!

像这样创建对象后:

// data is an associative array of user info that includes 'first_name' key
$user = new UserProfile($data); 

您可以通过以下方式提取名字:

$fname = $user->getFirstName();