Welcome Guest, Not a member yet? Register   Sign In
how to extend a class in controller
#1

[eluser]siubie[/eluser]
hi guys iam trying to extend a class in controller :
Code:
<?php

class ControlPanel extends Controller {
  var $Title;
  var $Heading;

    function ControlPanel(){
    parent::Controller();
        session_start();
        $this->load->model('web_model');
        $this->Title="Title";
        $this->Heading="Heading";
    }

    function index(){
          $data['Title']=$this->Title;
          $data['Heading']=$this->Heading;
          $data['CuapCuap']="Welcome" . $this->Heading;
          $this->load->model('web_model');
      $this->load->view('default',$data);
    }

  }

?>
then i try to extend ControlPanel class to ControlPanelExtend
Code:
<?php

class ControlPanelExtend extends ControlPanel {
  var $Title;
  var $Heading;

    function ControlPanelExtend(){
    parent::Controller();
        session_start();
        $this->load->model('web_model');
        $this->Title="Title";
        $this->Heading="Heading";
    }

    function index(){
          $data['Title']=$this->Title;
          $data['Heading']=$this->Heading;
          $data['CuapCuap']="Welcome" . $this->Heading;
          $this->load->model('web_model');
      $this->load->view('default',$data);
    }
        function logout(){
          session_destroy();
          redirect('controlPanel','refresh');
        }

  }

?>

i got error code:
Fatal error: Class 'ControlPanel' not found in F:\xampp\htdocs\code\system\application\controllers\ControlPanelAdmin.php on line 3

is iam doing wrong about this ?
#2

[eluser]pistolPete[/eluser]
You have to include the parent class file:

Either use
Code:
<?php
// be sure to use the correct path here.
require_once('controlpanel.php');
class ControlPanelExtend extends ControlPanel { (...)

Or autoload your class "ControlPanel".
#3

[eluser]pehaw[/eluser]
Also, consider your code structure. You have duplicated many things from the superclass in the subclass.

You can clean up your subclass to look like the following

NB - you also need to make sure you are calling the correct super constructor in ControlPanelExtend - ie ControlPanelExtend's constructor rather than Controller's.

Code:
<?php
require_once('controlpanel.php');
class ControlPanelExtend extends ControlPanel
{
    function ControlPanelExtend()
    {
        parent::ControlPanel();
    }

    function logout()
    {
        session_destroy();
        redirect('controlPanel','refresh');
    }
}

?>
#4

[eluser]siubie[/eluser]
yeah thanks a lot guys
its really help me Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB