1

I am really new to Codeigniter, and just learning from scratch. checked the documentation on Creating Libraries but no success on my example:

I need to pass a value to __construct library.

class: libraries/Myclasses/Bird

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Bird{
 public $fly;
 public $goodsound;

 public function __construct($fly, $goodsound) {
    $fly = $this->fly;
    $goodsound = $this->goodsound;
 }
 public function sentance(){
    return "This Bird can ".$this->fly . " and has ". $this->goodsound;
 }
}

class: libraries/Mybird

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed'); 
require_once(APPPATH.'libraries/Myclasses/Bird.php');

class Mybird extends Bird {
  public function __construct() {
  }
}

controller: Birds

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Birds extends CI_Controller {
 public function __construct(){
    parent::__construct();
    $config = array('fly' => 'fly', 'goodsound' => 'very good');
    $this->load->library('Mybird', $config);
 }

 public function index(){
    $mybird = new mybird();
    echo $mybird->sentance();
 }
}

I think that the problem is in Mybird class that not passing the values but i can't figure out how to handle it.

8
  • why are you loading library Mybird twice in controller birds? Once with and once without $config??? Commented Oct 16, 2017 at 17:35
  • it was just a try .. but for sure no success! Commented Oct 16, 2017 at 17:37
  • I think firstly your library Mybird also needs to be set to except parameters in its constructor when the library is called: Commented Oct 16, 2017 at 17:42
  • I tried to put them inside parent::__construct(); I dont know if this is the correct way ... but when i do that I cant get the controller values! .. i really need who can correct me! Commented Oct 16, 2017 at 17:45
  • Just curious, what is the reason for needing to pass parameters to __construct()? Commented Oct 16, 2017 at 17:48

2 Answers 2

2

One issue is in the constructor for Bird. Try this.

class Bird{
    public $fly;
    public $goodsound;

    public function __construct($fly, $goodsound)
    {
        $this->fly = $fly;
        $this->goodsound = $goodsound;
    }

Without the $this-> before the property name you are creating local variables that will go out of scope when the constructor ends.

Secondly, any class extending Bird should pass two arguments to the base class constructor. For instance:

class Mybird extends Bird {
  public function __construct() 
  {
      parent::__construct('Fly', 'very good');
  }
}

You could define Mybird to accept arguments too and then pass those to parent::__construct

class Mybird extends Bird {
  public function __construct($fly, $goodsound) 
  {
      parent::__construct($fly, $goodsound);
  }
}

There is no need for the new call in the Controller - $this->load->library('Mybird', $config); did that for you already.

index() should work fine as shown below. Note that Mybird is a property of the controller and so needs to be accessed using $this.

 public function index(){
    echo $this->Mybird->sentance();
 }

However, if you want to pass a $config array as an argument when loading a library then you need to revise both the Bird and the Mybird classes like so.

class Bird
{
    public $fly;
    public $goodsound;

    public function __construct($config)
    {
        $this->fly = $config['fly'];
        $this->goodsound = $config['goodsound'];
    }

}


class Mybird extends Bird
{
    public function __construct($config)
    {
        parent::__construct($config);
    }

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

Comments

0

Your library Mybird also needs to be set to except parameters in its constructor when the library is called from the controller Bird with $this->load->library('Mybird', $config);

when extending the library, you need to stick to what is set in your config.php, something like $config['subclass_prefix'] = 'MY_'; would require MY_Bird instead of Mybird

more info on the subject here and here

1 Comment

I can call itself in the same class ? is it working? you mean like that? class Mybird extends Bird { public function __construct() { $this->load->library('Mybird', $config); } }

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.