I try to access to a protected property with inheritance class but when I get my value property with $this->getContainer(), I got NULL value and I don't know why ...
I very simplified my code :
<?php
abstract class Kernel
{
protected $container;
public function __construct() {
$this->setContainer(['config' => 'OK']);
}
public function setContainer($array) {
$this->container = $array;
}
public function getContainer() {
return $this->container;
}
}
class AppKernel extends Kernel {
}
class FrontController extends AppKernel
{
public function __construct() {
var_dump($this->getContainer());
}
}
// Init
$kernel = new AppKernel();
$FrontController = new FrontController();
Normaly, when I call new FrontController, it should print my array in my protected property, but i got NULL.
Someone can help me please ?
Thanks!
you are overriding the default constrcuctor.
add parent::__construct to the front controller constructor
Related
I have this code and something seems wrong to me. The fact that I have to assign the same value to two different attributes. One from my trait and the other from my current class.
I wish I could completely isolate my trait and not have to make this assignment in my child class constructor.
Code:
interface ARepoInterface extends BaseRepoInterface {}
interface BRepoInterface extends BaseRepoInterface {}
trait Foo {
protected BaseRepoInterface $repo;
public function method(array $array): void {
// Do stuff with $repo
}
}
class A
{
private ARepoInterface $ARepo;
protected BaseRepoInterface $repo;
use Foo;
public function __construct(ARepoInterface $ARepo)
{
//#todo This is weird
$this->ARepo = $this->repo = $ARepo;
}
//Other methods
}
class B
{
private BRepoInterface $BRepo;
protected BaseRepoInterface $repo;
use Foo;
public function __construct(BRepoInterface $BRepo)
{
//#todo This is weird
$this->BRepo = $this->repo = $BRepo;
}
//Other methods
}
Thank you in advance for your advice
In fact PHP doesn't care much about the type hinting, so one property is enough for you.
interface ARepoInterface extends BaseRepoInterface { public function A(); }
class A
{
use Foo;
public function __construct(ARepoInterface $ARepo)
{
$this->repo = $ARepo;
}
public function methodDoStuffWithARepoInterface()
{
$this->repo->A();
}
}
And don't worry, intellisense still works.
Say I have the following code, is there a way to somehow extend an abstract class on a child and require a different type of argument in the "overloaded" function. I want to insert various types of objects in the Collection through the add function. In some cases, I'd like to insert an Error object, sometimes some other (XYZ) object, and let's say that all those objects extend the same abstract class called Parent.
I would appreciate if somebody could tell me if something like this is even possible, and if it is suggest some ways to accomplish this. Note that production server on which I intend to host the application runs on php 5.6.40.
Thank you in advance.
namespace App;
use App\Models\Parent;
abstract class Collection
{
protected $collection;
public function __construct()
{
$this->collection = array();
}
abstract public function add($key, Parent $item);
}
public class ErrorList extends Collection
{
public function __construct()
{
parent::__construct();
}
public function add($key, Error $item)
{
$this->collection[$key] = $item;
}
}
namespace App\Models;
abstract class Parent {}
public class Error extends Parent {}
public class XYZ extends Parent{}
Try this
abstract class Collection
{
protected $collection;
public function __construct()
{
$this->collection = array();
}
//no type hinting
abstract public function add($key, $item);
}
class ErrorList extends Collection
{
// this constructor doing nothing , it can be removed and
// parent constructor will still be called unlike java or any other
// OOP
public function __construct()
{
parent::__construct();
}
//no type hinting
public function add($key, $item)
{
//code
}
}
If you're extending a class or implementing an interface the signature must match. You can however implement type checking yourself and type hint in a docblock.
As a side note, public class is invalid syntax.
abstract class Collection
{
protected $collection;
public function __construct()
{
$this->collection = array();
}
abstract public function add($key, Parent $item);
}
class ErrorList extends Collection
{
public function __construct()
{
parent::__construct();
}
/**
* #param $key
* #param Parent|Error $item
*/
public function add($key, Parent $item)
{
if (!($item instanceof Error)) {
throw new \InvalidArgumentException('Unable to add object to error list: ' . get_class($item));
}
$this->collection[$key] = $item;
}
}
<?php
class BaseController extends Controller
{
protected $foo;
public function __construct()
{
$this->foo = '123';
}
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
Above is the BaseController and I want to declare foo as 123, but can I get the foo variable in the controller which I have extended from this basecontroller, can you help?
public function detail($action)
{
return $this->foo;
}
As per docs:
http://php.net/manual/en/language.oop5.decon.php
Note: Parent constructors are not called implicitly if the child class
defines a constructor. In order to run a parent constructor, a call to
parent::__construct() within the child constructor is required.
As you are doing some work in your parent class constructor, you must invoke it directly in your subclass too (even this would be to only thing you do in child's constructor). I.e.:
class ChildController extends BaseController
{
public function __construct() {
parent::__construct();
}
...
When you extend the controller, I imagine that you're currently doing this:
<?php
class NewController extends BaseController
{
public function __construct()
{
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}
You see how the __construct method is being overwritten. You can easily fix this by adding parent::__construct() to the beginning of the method, so you'll have this:
<?php
class NewController extends BaseController
{
public function __construct()
{
parent::__construct();
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}
I found something about this error but I think this is little bit different. I defined a public variable.
Class Controller{
public $model;
And I'm trying add extra word(model) between $model_name and $this.
public function call_model($model_name){
$this->model->$model_name = new $model_class;
What is the solution?
EDIT:
Warning: Creating default object from empty value in C:\xampp\htdocs\alisveris\project_library\Controller.php on line 16
You can create static factory method to create different models like this:
abstract class Model {
static function CallModel($name) {
switch ( $name ) {
case 'Naomi': return new NaomiCampbell();
case 'Anja': return new AnjaRubik();
default: return new $name;
}
}
}
class NaomiCampell extends Model {}
class AnjaRubik extends Model {}
Then use:
$MyModel = Model::CallModel($name);
You can do it by assigning $model variable to $this
class Db
{
public function great()
{
echo 'great';
}
}
class Controller{
public $model;
public function __construct()
{
$this->model = $this;
$model_name = 'Db';
$this->model->$model_name = new $model_name;
}
}
$cc = new Controller();
echo $cc->model->Db->great();
Output
great
I need to call a function someFunction() how do I refer to it when it is in the following class structures?
abstract class A
{
protected $session;
protected $model;
public function __construct()
{
$session = new classSession;
$model = new classModel;
}
}
class classModel
{
$this->session->someFunction();
}
I've tried using $this->session->someFunction() but it does not work!
Firstly, you need to put it within the context of a function - code cannot exist on its own within a class body. Secondly, for anything in classModel to access $session, classModel has to extend class A. You end up with something like this:
class classModel extends A
{
public function foo()
{
$this->session->someFunction();
}
}
So for $this->session->someFunction(); to execute, you'd do this:
$model = new classModel();
$model->foo();