PHP - How to extend and access parent constructor properties - php

I am attempting to access the parent class __construct properties within a child class that extends this, however not sure how to do this as I have tried multiple methods and didn't give me the expected result.
So I have a baseController and a indexController which extends it, I want to be able to have direct access to the properties of the parent within the child controller.
$config = ['site' => 'test.com'];
class baseController {
public function __construct($config){
$this->config = $config;
}
}
class indexController extends baseController {
public function __construct(){
parent::__construct(); // doesnt seem to give any outcome
}
public static function index() {
var_dump($this->config); // need to access within this method
}
}
$app->route('/',array('indexController','index')); // the route / would call this controller and method to return a response

There are several issues with code you have there. You are setting up config as a global, it should be inside your BaseController and set it to public or protected:
class BaseController {
protected $config = ...
Just like #mhvvzmak1 mentioned, your child constructor is calling the parent properly. for example you can do it like so:
class IndexController extends BaseController {
public function __construct(){
$config = [];
parent::__construct($config);
}
and finally just like dan08 mentioned, you can't reference $this from a static method, change your index function:
public function index() {
Update
If you really want the child function to remain static as required by your framework, you make config a static function on the BaseController and call it in the child.
class BaseController {
protected static function config() {
return ['site' => 'mySite'];
}
}
class Child extends BaseController {
public static function index() {
$config = BaseController::config();
}
}

Related

How to load a model in a master controller on codeigniter framework?

I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().

In codeigniter how to get a reference to the child controller from a method in parent controller when that method is called from the child controller

Suppose I have two child controllers of MY_Controller,say Child_1 & Child_2.In MY_Controller I have two methods method_1 & method_2 as
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
some code ...
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I am calling method_2() from child_1 & child_2 using
class Child_1 extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function method_1(){//custom implementation goes here}
public function some_method()
{
...some code
$this->method_2();//call inherited method method_2()
}
}
A similar code for child_2
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
// some code ...
$this->method_1(); //Call the method directly it will implement the child's method
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}

my base_url is not working although i autoloaded it..why?

$autoload['helper'] = array('url', 'form');
class user extends CI_Controller
{
function user()
{
$this->view_data['base_url']= base_url();
}
function index()
{
$this->regester();
}
function regester()
{
$this->load->view('user_view',$this->view_data);
}
}
Try using a proper constructor for php5:
class User extends CI_Controller
{
private $view_data;
public function __construct()
{
parent::__construct(); // necessary in CI if you declare a constructor
$this->view_data['base_url']= base_url();
}
}
For backwards compatibility, if PHP 5 cannot find a __construct()
function for a given class, and the class did not inherit one from a
parent class, it will search for the old-style constructor function,
by the name of the class
Since your class is a child of CI_Controller, which has a __construct() method, I believe the method User() in your class isn't being called upon instanciation, and so does base_url()

How to use $layout in Laravel controller

I try to use the $layout property in my controllers, but I always get the following error:
Call to a member function nest() on a non-object
When dd() the value of this->layout, it seems like it is only a string.
Here's my code:
base.php
class Base_Controller extends Controller {
public $layout = 'layouts.common';
}
admin.php
class Admin_Controller extends Base_Controller {
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}
I probably miss something, but I can't find it in the docs
Ahhh, just found the trouble. I was ovewritting __construct to add filters, but wasn't calling the parent construct:
class Admin_Controller extends Base_Controller {
public function __construct() {
parent::__construct();
$this->filter('before', 'auth');
}
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}

Determining Proper Class Scope PHP

I have a model class ModelHome that is a child of Model ie:
class ModelHome extends Model
Model is a variable of the Controller class ie:
class Controller {
public $model;
public function __construct () {
$this->model = new Model;
}
}
Is it possible to access a method within the Controller class from within a method inside the ModelHome class?
I've tried parent:: and calling the class by name ie Controller::method but I can't seem to find the right scope to access the method I need.
Thanks.
-Vince
First of all, you must have an instance of ModelHome. If you make an instance of Model, that has not automatically been extended by ModelHome just because ModelHome exists. So, i guess your Controller::__construct() should be:
public function __construct () {
$this->model = new ModelHome;
}
However, your ModelHome does not know about your Controller class/instance. You could make a __construct in ModelHome that takes a parameter with a link to the controller. Like this:
class ModelHome extends Model {
public $controller;
public function __construct ($controller) {
$this->controller = $controller;
}
}
class Controller {
public $model;
public function __construct () {
$this->model = new ModelHome($this);
}
}
Now, your ModelHome knows about the controller by using $this->controller

Categories