2017-08-28 146 views
0

我目前使用PIMAX对于Facebook信使:https://github.com/pimax/fb-messenger-php从类访问属性 - PHP

我想从类UserProfile访问first_name领域UserProfile.php,并将其存储在一个变量,这样我可以使用名称用于个性化消息给用户。我很难做到这一点,而不会破坏机器人,并会感谢您的帮助!

这里的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']; 
    } 
} 

预先感谢您的帮助!

+0

你在哪里创建这个类的一个实例:

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

您可以用提取的第一个名字吗?防爆。 '$ up = new UserProfile($ data); $ up-> getFirstName();' 你当然需要自己提供$ data数组。 –

回答

2

一旦你创建你的对象是这样的:

$fname = $user->getFirstName();