Notice: Undefined property: In creating new model using namespace

Notice: Undefined property: In creating new model using namespace



I am a newbie php coder. I have this main controller:


namespace AppCore;

class Controller

/** @var View View The view object */
public $View;
public $templates;
/**
* Construct the (base) controller. This happens when a real controller is constructed, like in
*/
public function __construct()



public function loadModel()

$this->model = new AppFrontModelIndexModel(); //error line





In IndexController I have:


namespace AppFrontController;

use AppFrontModelIndexModel;

class IndexController extends AppCoreController

public function index()
$this->loadModel->test();




In IndexModel I have:


namespace AppFrontModel;

class IndexModel


public function test()

echo 'test print';





In Action I get this error:



Notice: Undefined property: AppFrontControllerIndexController::$loadModel in /Applications/xampp/htdocs/cmstest/application/Front/Controller/IndexController.php on line 13



I load all classes using composer and PDR-4 method.



What is the problem and how do I fix it? Thanks






Controller::loadModel is a method not a property so, $this->loadModel->test(); should be $this->loadModel()->test();

– CD001
Sep 12 '18 at 12:56



Controller::loadModel


$this->loadModel->test();


$this->loadModel()->test();






@CD001: I change this But see error:Fatal error: Uncaught Error: Call to a member function test() on null in /Applications/xammp/htdocs/cmstest/application/Front/Controller/IndexController.php:13

– harmony talk
Sep 12 '18 at 13:01



Fatal error: Uncaught Error: Call to a member function test() on null in /Applications/xammp/htdocs/cmstest/application/Front/Controller/IndexController.php:13






loadModel() needs to return the IndexModel you've just assigned to $this->model - e.g. public function loadModel() $this->model = new AppFrontModelIndexModel(); return $this->model If you don't return anything, it's NULL

– CD001
Sep 12 '18 at 13:04



loadModel()


IndexModel


$this->model


public function loadModel() $this->model = new AppFrontModelIndexModel(); return $this->model


NULL






@harmonytalk - did my post answer you?

– dWinder
Sep 12 '18 at 13:17






@DavidWinder: Sure. u see my comment below your answer.

– harmony talk
Sep 12 '18 at 13:28




1 Answer
1



Notice that in your loadModel method you just assign the new model to this but you not returning anything -> so you cannot can function test() on null.


loadModel


this


test()



In order to fix it use:


class IndexController extends AppCoreController
public function index()
$this->loadModel();
$this->model->test();




If you insist on doing in index only one function you can modify your loadModel function to:


index


loadModel


public function loadModel()
if (!$this->model) // that way you load the Model only once. If you want to reload every time just remove the if
$this->model = new AppFrontModelIndexModel();
return $this->model;



And then you can do:


public function index()
$this->loadModel()->test();






This should also work, yup.

– CD001
Sep 12 '18 at 13:10






this worked. My way is true and good for load two object in IndexController?

– harmony talk
Sep 12 '18 at 13:25


IndexController






Not sure I follow, you mean to "load" = set 2 property in the loadModel()` function?

– dWinder
Sep 12 '18 at 13:37






@DavidWinder: my mean separate two $this->loadModel(); $this->model->test();

– harmony talk
Sep 12 '18 at 13:47


$this->loadModel(); $this->model->test();






If you mean to call 2 different function then yes

– dWinder
Sep 12 '18 at 14:29



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)