[如何在php7中将父数据对象扩展到子对象以访问父数据Symfony 5

问题描述 投票:0回答:1

我正在使用帮助程序文件来获取页面特定的数据,但是我使用baseHelper文件来获取站点范围的数据。我以前是依赖注入基础帮助程序来访问数据信息的,如下所示:

namespace App\Helpers\Users\Profiles;

use App\Core\BaseHelper;
use App\Services\data\SocialService;

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;
    }
}

由于页面需要更多数据,因此注入baseHelper变得不可读,因此我试图将baseHelper文件直接扩展到profileHelper文件中,因此我可以在没有依赖项注入的情况下检索数据对象。但是,当我尝试加载页面时,我得到的是未定义的属性$ data,它是从baseHelper $ this-> data对象返回的,该对象在检查用户时包含用户数据。

用于调用用户功能的BaseHelper文件:

namespace App\Core;

use App\Services\security\SecurityService;
use App\Services\users\UserService;

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获取页面特定数据:

namespace App\Helpers\Users\Profiles;

use App\Core\BaseHelper;
use App\Services\data\SocialService;

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;
    }
}

我已经提取了相关代码来显示错误的示例,主文件中注入了更多服务文件,因此为什么我要扩展basehelper来访问data属性。我正在使用symfony5并专门注入文件。

symfony inheritance dependency-injection php-7
1个回答
0
投票

[您不能在构造父类的同时构造子类。在您的孩子中,呼叫parent::_construct()并输入所需的参数。然后将定义$data(以及$user$sec)。

© www.soinside.com 2019 - 2024. All rights reserved.