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.

Mina Magdy
  • 141
  • 1
  • 15
  • why are you loading library Mybird twice in controller birds? Once with and once without $config??? – Vickel Oct 16 '17 at 17:35
  • it was just a try .. but for sure no success! – Mina Magdy Oct 16 '17 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: – Vickel Oct 16 '17 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! – Mina Magdy Oct 16 '17 at 17:45
  • Just curious, what is the reason for needing to pass parameters to __construct()? – Goose Oct 16 '17 at 17:48
  • also don't extend your libray, make one only, if you call $this->load->library('Bird', $config); it should work – Vickel Oct 16 '17 at 17:48
  • @Vickel I would like to do that but I need to extend to CI_Controller. this is my problem. – Mina Magdy Oct 16 '17 at 17:52
  • @Goose I need it because the In real library its bring data for json using this way!!! – Mina Magdy Oct 16 '17 at 17:55

2 Answers2

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);
    }

}
DFriend
  • 8,869
  • 1
  • 13
  • 26
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

Vickel
  • 7,879
  • 6
  • 35
  • 56
  • 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); } }` – Mina Magdy Oct 16 '17 at 17:49