Welcome Guest, Not a member yet? Register   Sign In
Setting up CodeIgniter to load system from composer package
#11

[eluser]Flemming[/eluser]
I'm definitely interested! And I'm really pleased to see that people are still doing new things with CI despite all the chatter around its stagnation - it gives me hope for the future of CI :-)
#12

[eluser]stevezissou[/eluser]
Sorry, but I don't really see the point. Composer already handles autoloading for you. All you need to do is utilize PHP's built-in aliasing ('use') syntax. Why add more work to loading composer vendor classes?

http://php.net/manual/en/language.namesp...orting.php
#13

[eluser]dmyers[/eluser]
That works good if you need a new instance of a composer class each time but, how do you handle singleton's using your method?


Also I was going to show how to load CodeIgniter as a Composer Package. Since that's not namespaced. You can't use the method you outlined. I thought it would help users. Since I am running CI 3.0 dev to get the latest changes all I need to do is type "composer update". I also load my CodeIgniter Packages using the same method. This allows me to keep all of my packages in sync using the same GIT repro.
#14

[eluser]Narf[/eluser]
[quote author="dmyers" date="1409247008"]That works good if you need a new instance of a composer class each time but, how do you handle singleton's using your method?
[/quote]

Code:
$this->library_name = new OptionalNamespace\ClassName($foo, $bar);
#15

[eluser]dmyers[/eluser]
Code:
$this->library_name = new OptionalNamespace\ClassName($foo, $bar);

Is good but if you use that in another place you get another instance.

Where with the CodeIgniter Library Loader you can call

Code:
$this->load->library('ClassName');

All day and

Code:
$this->ClassName

is still the same instance.

So then you say "well just test to see if it's already loaded" with something like this every where it needs to be "loaded"

Code:
if (!is_object($this->library_name)) {
  $this->library_name = new OptionalNamespace\ClassName($foo, $bar);
}

Which is what my "extended" loader already does.

Code:
$this->load->library('OptionalNamespace\ClassName');

Saving the end user form typing

Code:
if (!is_object($this->library_name)) {
  $this->library_name = new OptionalNamespace\ClassName($foo, $bar);
}

Over and over.

Nice thing is the extended loader also works with the $autoload config as well.
#16

[eluser]dmyers[/eluser]
also I'm surprised your recommending attaching something directly to the CI class. The codeigniter loader will overwrite it if you try to load another class with the same name. where if you use the loader (my extended code) it will throw a error just like CI already does when you try to load a matching class
#17

[eluser]Narf[/eluser]
[quote author="dmyers" date="1409336188"]
Code:
$this->library_name = new OptionalNamespace\ClassName($foo, $bar);

Is good but if you use that in another place you get another instance.

Where with the CodeIgniter Library Loader you can call

Code:
$this->load->library('ClassName');

All day and

Code:
$this->ClassName

is still the same instance.

So then you say "well just test to see if it's already loaded" with something like this every where it needs to be "loaded"

Code:
if (!is_object($this->library_name)) {
  $this->library_name = new OptionalNamespace\ClassName($foo, $bar);
}

Which is what my "extended" loader already does.

Code:
$this->load->library('OptionalNamespace\ClassName');

Saving the end user form typing

Code:
if (!is_object($this->library_name)) {
  $this->library_name = new OptionalNamespace\ClassName($foo, $bar);
}

Over and over.

Nice thing is the extended loader also works with the $autoload config as well.
[/quote]

$this->load->library() was not made for the "benefits" that you're listing, they are rather side effects.

A more modern version of CI, that doesn't support PHP 5.2, would no longer have the load->library() method, nor would it need configuring an autoloader. Those two, along with the singleton pattern are legacy that we'd want to break away from at some point.
#18

[eluser]Narf[/eluser]
[quote author="dmyers" date="1409336953"]also I'm surprised your recommending attaching something directly to the CI class. The codeigniter loader will overwrite it if you try to load another class with the same name. where if you use the loader (my extended code) it will throw a error just like CI already does when you try to load a matching class[/quote]

You asked how a thing could be done and I answered.
Name collisions are something that you can't get away from. Smile
#19

[eluser]dmyers[/eluser]
I see so my original offer to help others learn how to load composer packages using already established CodeIgniter conventions is pretty worthless since in the next 2-5 years CodeIgniter will no longer need to use load->library(). I was not aware CI was headed in that direction. Offer and further help rescinded.




Theme © iAndrew 2016 - Forum software by © MyBB