Welcome Guest, Not a member yet? Register   Sign In
Creating and using libraries
#1

[eluser]Knitter[/eluser]
Hi,

I've created a few libraries, since that was the only way I found to have my classes work properly withing CI, but I'm having problems loading some of them.

Being libraries I followed the docs for their creation, but one of them is not loading correctly, or not loading at all. All of them are in the autoload array.

I have a library name Toolbar, in it I create an array with Menu objects. The Toolbar class loads correctly, but the Menu class will always fail.
The Toolbar's constructor is something like:
Code:
public function  __construct() {
        $barra = array();

        $barra[] = new Menu('Início', 'inicio', false);
        $barra[] = new Menu('Automóveis', 'automoveis', false);
        $barra[] = new Menu('Eventos', 'eventos', false);
        ....

With this constructor I have the classic "Fatal error: Class 'Menu' not found".

If I comment out the use of the Menu object, and I leave the Menu class in the autoload array for libraries I get
Quote:Severity: Warning

Message: Missing argument 1 for Menu::__construct(), called in (...)/libraries/Loader.php on line 928 and defined

Filename: libraries/Menu.php

I understand that CI is trying to instantiate my class, or am I wrong? If so, why is CI trying to create an instance of the object, is the autoload responsible for creating the instances?

How do I get around this without having to include every php file with the classes I need? I would really prefer to use my own autoload special function that would load every class I use, instead of having to manually include the files, but that, I think, would mess up CI.
Any suggestions?
#2

[eluser]Damien K.[/eluser]
Not sure how CI does autoloading, ie. whether they instantiate the class upon loading. Feel free to look at the source code... AND let us know. Wink

Make sure you load your Menu class before you load your Toolbar class in the autoload array as they are loaded in priority sequence. If that doesn't work, maybe it failed to autoload. It looks like you have a mandatory argument for your Menu constructor; make it an optional argument.
#3

[eluser]Knitter[/eluser]
I'm going over the problem by including the file for the Menu class in the Toolbar class, not what I wanted but for now it work. CI is actually trying to instantiate the Menu class, but that provides another problem, I want several, different, instances for that class as each instance is a menu item.

And if the arguments are mandatory, why would I make them optional? It just doesn't make sense to have a Menu instance that is not properly initiated, and that doesn't have its main attributes properly set, and no, creating an init function, or similar, to initiate them after instantiation is not the way to go.

Regards,

Knitter
#4

[eluser]Damien K.[/eluser]
This is PHP and not Java Smile. It's okay to do all sort of scriptish stuff in your code. Big Grin
#5

[eluser]Knitter[/eluser]
Did I ever mentioned Java? For crying out loud this is right on the annoying part, can't I just write some classes and use the scripting where it makes sense and objects where they make sense? You don't even know the project, I'm sorry if I'm being harsh, and I hope it's just me, but I just got the worse impression of this forum and its users.

Maybe I should also tell that I develop in Perl, Python, C#, Groovy, C, C++ even Assembly, I can pretty well separate things, or is asking questions not appreciated here? I don't know, or only the "right" questions may be asked? I may be asking questions that seem simple and direct to all you CI users, and maybe it's just me... no it must really be me as I can't recall a forum post I have made without having some user telling me to complain in someone else yard.

It looks like I've been labeled the "Java troll"... I sincerely thanks all of you who took the time to read all my posts, but from what I see I think this will be the only topics I'll have here.

Regards,

An annoyed and Knitter
#6

[eluser]louis w[/eluser]
When you autoload a library its basically doing this:

include('libraries/foo.php');
$this->foo = new Foo();


With libraries you don't use 'new' as CI has already created it, just call the methods. It's not a class in the normal sense of one, where you can instantiate it multiple times.

I hopes this helps you.
#7

[eluser]BrianDHall[/eluser]
Sorry you've had a rough run in the last day or so Knitter. Forums seem to have personalities all their own, and with it they get their own mood swings. In the last few days I've noticed the tone unusually terse, nasty in a few instances, and tending towards not especially helpful to new users. This seems to have some relation with an unusual amount of repetition in topics and questions lately, and has spilled over into threads with unique questions.

I would think this would be less now adays with so many options for self-expression - twitter, facebook/myspace, blogging, endless selections of mailing lists and forums...etc, etc...but it doesn't. I'm not sure why people still insist that crackers should taste like a cheeseburger (wikis, forums, chat rooms, blogs, miniblogs, and social networks all exist for DIFFERT things), but they do.

To answer your question, if I may...

Your problem is with auto-loading. By auto-loading anything CI is include-ing the source code for the class, and then loading it and assigning it as a member object of the CI super-object (you can checkup on get_instance() to access this super-object directly without using $this in the controller).

This is why if you autoload session, you can access it with $this->session->... - session is actually an instantiated object.

I am not aware of any way to pass parameters to an auto-loaded anything, and worse-yet you cannot create multiple instances of something just because you auto-loaded it.

Instead, what you will want to do is go back to the pure PHP function of include, and include your Menu class.

You will then be able to use new Menu() precisely as OOP in PHP5 works. CI has no helper functions that I am aware of to help automate class definition inclusion in this way - the load feature only works when you want one of something.

With this relatively easy change, you will find your code should then work precisely as you think it would.
#8

[eluser]Knitter[/eluser]
Thanks, that is the "workaround" I currently have, I'm trying to change CI to include an __autoload function that will get all those manual includes away. That was, I think, my biggest surprise, something as simple as the __autoload magic function is not present in CI? Or have I missed it lurking around?


Regards,

Knitter
#9

[eluser]BrianDHall[/eluser]
[quote author="Knitter" date="1254449070"]Thanks, that is the "workaround" I currently have, I'm trying to change CI to include an __autoload function that will get all those manual includes away. That was, I think, my biggest surprise, something as simple as the __autoload magic function is not present in CI? Or have I missed it lurking around?


Regards,

Knitter[/quote]

I must admit ignorance here, as far as I know CI doesn't provide any sort of helper comparable to __autoload magic functioning. Its PHP so you can implement something of the sort, but I don't know.

Over in the DMZ thread in the Ignited Code forum, the creator is Overzealous. You might check with him directly as he uses this very function of magic functioning, so when you call $user-usergroup it somehow intercepts this call at run time and loads the model and assigns it to the $user variable. Damned if I know how it works.
#10

[eluser]InsiteFX[/eluser]
Hi Knitter,

Please do not say that the forum is bad, look at how many posts that user has!

All most all of the users here are always glad to help you in any way that we can.

BrianDHall makes lots of post on this forum and helps others to understand CodeIgniter.

I for one come from the same coding world you come from. So I understand your feelings.

Enjoy and have a nice day.
InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB