How to extend parent data object to child object in php7 to access parent data Symfony 5

I am using a helper file to get page specific data, but I use a baseHelper file to get site wide data. I was previously dependency injecting the base helper to access the data information, as shown below:

namespace AppHelpersUsersProfiles;

use AppCoreBaseHelper;
use AppServicesdataSocialService;

class ProfileHelper
{
    public function __construct(BaseHelper $helper, AssetService $assets, ImageService $images, MediaService $media, SocialService $social)
    {
        $this->base = $helper;
        $this->data = $this->base->data;
        $this->assets = $assets;
        $this->social = $social;
        $this->images = $images;
        $this->media = $media;
    }

    public function getPageData()
    {
    $this->data->content->media = $this->media->getMedia($this->data->user->usercode);
    $this->data->content->images = $this->images->parseImages('all')->userImages;
    return $this->data;
    }
}

As the pages need more data it is becoming unreadable to inject the baseHelper so I am trying to extend the baseHelper file directly into the profileHelper file, so I can retrieve the data object without dependency injection. However when I try to load the page I am getting undefined property $data, which is meant to be returned from the baseHelper $this->data object which contains user data when the user is checked.

BaseHelper file used to call user functions:

namespace AppCore;

use AppServicessecuritySecurityService;
use AppServicesusersUserService;

class BaseHelper
{
    public function __construct(SecurityService $security, UserService $user)
    {
        $this->sec = $security;
        $this->user = $user;
        $this->data = $this->checkUserExists();
    }

    private function checkUserExists()
    {
        if ($this->sec->isLoggedIn()) {
            $this->identity = $this->sec->data();
            return $data = $this->user->parseUser($this->identity->getUserCode());
        } else {
            return $data = (object) [];
        }
    }
}

ProfileHelper to get page specific data:

namespace AppHelpersUsersProfiles;

use AppCoreBaseHelper;
use AppServicesdataSocialService;

class ProfileHelper extends BaseHelper
{
    public function __construct(AssetService $assets, SocialService $social)
    {
        $this->assets = $assets;
        $this->social = $social;
        $this->data = $this->data;
    }

    public function getPageUrls($urls)
    {
        $urls->js->pageJS = $this->assets->getJSUrl('modules/profile/profileDev.js');
        return $urls;
    }

    public function getPageData()
    {
        $this->data->content->posts = $this->social->getPosts($this->data->uname)
        return $this->data;
    }
}

I have extracted the relevant code to show the example of the error, the main files have a lot more service file injected in, hence why I am trying to extend the basehelper to access the data property. I am using symfony5 and injecting files specifically.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *