Welcome Guest, Not a member yet? Register   Sign In
Error when trying to get property from parent class
#1

It looks I'm missing something. I'm trying to get property value from parent class (BaseController) and im getting "Trying to get property 'saveUserLog' of non-object".
App\Controllers\UserLog

Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\UserLogModel;

class UserLog extends BaseController
{   
    private $userLogModel; 
    private $logObject;
    private $logAction;

    public function __construct(string $logObject = '', string $logAction = 'U')
    {
        $this->userLogModel = model('UserLogModel');
        $this->logObject = $logObject;
        $this->logAction = $logAction;
    }
   
    /**
    * save Log
    *
    * @param  string $obj
    * @return void
    */
    public function saveLog(array $data)
    { 
        if(! $this->siteCfg->saveUserLog){
            return FALSE;
        }
        $dataToSave = [
            'date'      => date('Y-m-d H:i:s'),
            'user_id'  => user_id(),
            'action'    => $this->logAction,
            'object'    => $this->logObject,
            'object_prev_ver' => json_encode($data)
        ];
        return $this->userLogModel->save($dataToSave);       
    }
}



App\Controllers\BaseController.php


Code:
class BaseController extends Controller
{

    protected $helpers = ['auth','url', 'form','site'];
    protected $dataOutput = [];
    protected $siteCfg;
    protected $template;
    protected $templateAdmin;

 
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);

//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
// E.g.: $this->session = \Config\Services::session();
        $this->session = service('session');
        $this->siteCfg = config('SiteConfig');     
        $this->isLoggedIn = logged_in();     

        $this->dataOutput['__seoDesc'] = $this->siteCfg->seoDefDesc;
        $this->dataOutput['__seoTags'] = $this->siteCfg->seoDefTags;       
        $this->dataOutput['__seoTitle'] = $this->siteCfg->siteTitle;
        $this->dataOutput['__mainMenu'] = $this->siteCfg->mainMenu;
        $this->dataOutput['__themeFolder'] = $this->siteCfg->templateFolder;
        $this->dataOutput['__logged_in'] = $this->isLoggedIn;
        $this->dataOutput['__themeAdminFolder'] = $this->siteCfg->templateAdminFolder;
       
    } 
}


App\Config\SiteConfig

Code:
<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class SiteConfig extends BaseConfig
{
    /**
    * Main vars
    */
    public $siteTitle  = 'Site Title';

    /**
    * Mail
    */
    public $siteEmail = '[email protected]';
   

    /**
    * Articles
    */
    public $defArtImage = '3c92e1438ebc787c4834d44e6f69471b.jpg';
    public $defArtImgPath = 'uploads/gallery/';   

    /**
    * Users
    */
    public $defUserAvatarImg = 'ico_avatar.png';
    public $defUserAvatarPath = 'uploads/avatars/';

    public $saveUserLog = true;

    /**
    * Template
    */
    public $templateFolder = 'layout';

    public $templateAdminFolder = 'neptune';
   
    /**
    * SEO
    */
    public $seoDefDesc = 'Seo description';
    public $seoDefTags = 'Seo tags';
   
   
}


Thanks in advance for any help.
Reply
#2

Change it from private to protected.

public
scope to make that property/method available from anywhere, other classes and instances of the object.

private
scope when you want your property/method to be visible in its own class only.

protected
scope when you want to make your property/method visible in all classes that extend current class including the parent class.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Looking at your BaseController it seems there is really no $saveUserLog property.
Reply
#4

(10-11-2021, 07:32 PM)paulbalandan Wrote: Looking at your BaseController it seems there is really no $saveUserLog property.

As I said before, maybe Im missing something but for saveUserLog is property from config object (App\Config\SiteConfig). In BaseController I have $siteCfg property and then im checking $this->siteCfg->saveUserLog.

InsiteFX Wrote:Change it from private to protected.
Which one? siteCfg property is protected.
Reply
#5

You said you are trying to access the property from the parent class, to which I assumed you defined it as an explicit property.

Anyway, move the initialization of $this->siteCfg from the initController method into the __construct method.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB