2

Hi i am trying to do single inheritance in Laravel 5.2 for Controllers.

This is my parent basecontroller class

 <?php

 namespace App\Http\Controllers\Moviesfrontend;

 use Illuminate\Http\Request;

 use App\Http\Requests;
 use App\Http\Controllers\Controller;


 class BaseController extends Controller
 {
     public function __construct($request)
     {
         //code to check whether city is set in session or not
         $selectedcity=$request->session()->get('selectedcity');

     }
 }

In BaseController class construct will fetch the city name stored in session. Now i want to access this city name stored in $selectedcity in my child class.

My child controller class is

  <?php

  namespace App\Http\Controllers\Moviesfrontend;

  use Illuminate\Http\Request;

  use App\Http\Requests;
  use App\Http\Controllers\Controller;

  class HomeController extends BaseController
  {

      public function __construct(Request $request)
      {

          parent::__construct($request);

      }

      public function index(Request $request)
      {
          echo $selectedcity;
      }
  }

Bedefault I guess parent class variables are accessible to child class but this is not working and i am getting error message that $selectedcity not found.

Kindly assist me accessing this variable in chlid class.

0

1 Answer 1

2

Your $selectedcity is a local variable. It only exists in the function scope of the constructor. It will be gone after the constructor is invoked. You need to assign it to an instance variable instead, e.g.

class BaseController extends Controller
{
    protected $selectedCity;

    public function __construct($request)
    {
        $this->selectedCity = $request->session()->get('selectedcity');
        …

However, that would make it accessible to any subclass derived from the BaseController. If that's what you want, do it as shown.

But from the code you show, the BaseController looks superfluous to have. Just remove the BaseController and move the constructor to the HomeController:

class HomeController extends Controller
{
    private $selectedCity;

    public function __construct($request)
    {
        $this->selectedCity = $request->session()->get('selectedcity');
        …

To access the instance variable, you have to fetch it via $this, e.g.

public function index(Request $request)
{
    echo $this->selectedCity;
    …

Make sure to read the chapter on Object Inheritance in the PHP Manual if these topics are unclear to you.

Sign up to request clarification or add additional context in comments.

4 Comments

I did same thing as you said still its showing me Undefined Variable : selectedcity , and i have 4-5 controllers where i have written code to check whether session has got the value or not, so i was trying to create parent class in which i want to write the code to check session value is set or not so i created BaseController .
@dollar if you still get this error, you did something wrong. You are probably missing a $this somewhere. Or you did not create an instance property. In any case, this is inheritance 101. See this example as a proof of concept: codepad.org/Fm9Qu54h
gr8 thanks it worked, same way can i write functions in base class and access them in chlid class...?
@dollar yes, like I said: Make sure to read the chapter on Object Inheritance in the PHP Manual if these topics are unclear to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.