Search code examples
phplaraveloopfacebook-messengerfacebook-messenger-bot

Accessing property from class - php


I am currently using pimax for facebook messenger: https://github.com/pimax/fb-messenger-php

I would like to access the first_name field from the class UserProfile in UserProfile.php and store it in a variable so that I can use the name for personalizing messages to users. I am having a hard time doing that without breaking the bot and would appreciate your help!

Here's the UserProfile.php file:

<?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'];
    }
}

Thanks in advance for your help!


Solution

  • Once you've created your object like this:

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

    You can extract the first name with:

    $fname = $user->getFirstName();