CodeIgniter Forums

Full Version: Problem -> Acess to CI instance...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Multisnet[/eluser]
Hello, I need some help from CI experts. I'm from development team of a a Portuguese Webdesign company. We are using this framework for the first time (in a large scale project). As you could imagine we are facing many conceptual problems. :gulp:

I'll try to explain my doubt as clearly as possible.

I have a model call "new.php", inside that model (using Active Record) I implemented the general methods like getNew, setNew, saveNew, etc.
I want to implement methods for working with newS like getLastNews($number), I'm using a common methodology for object oriented programing which consist in implement this methods as static methods (in this example the method getLastNews($number) is a static method for the class New). The main idea is that the access to database should be processed only in the data layer (models).

Inside the method getLastNews($x) I need to call some CI lib functions like:
Code:
db->limit($x);
db->order_by("Date", $order);

I have a controller for the News in which I call the static method:
Code:
$this->load->model('new');
New::getLastNews($x);

My question is, how to call the CI functions like db->limit(). I couldn't use $this->db->limit() because the system returns this error "Using $this when not in object context". Which is obvious correct because I didn't instantiate any object of type New.

I could not use get_instance() (inside the static method) to access CI because this functions returns the controller and result in the error:
"Undefined property: News_controller::$db".

Have you ever face this problem? Could you make some suggestions please. I need to quickly solve this problem to go on with this project. I don't want to instantiate a object of type new to access to methods like getLastNews(), which deal with multiple object instances.

Thank you in advance...

El Forum

[eluser]tonanbarbarian[/eluser]
Your problem is how you are calling the getLastNEws method
you should do the following

Code:
$this->load->model('new');
$this->new->getLastNews($x);

You also need to make sure that your New class has a valid constructor so that it can inherit the $this->db and other related properties and methods from the controller correctly.

El Forum

[eluser]n0xie[/eluser]
Could you explain why you want to declare your methods as 'static'?

The $this->load function will automatically instantiate your model, thus making static methods pointless.

El Forum

[eluser]Multisnet[/eluser]
The idea behind this approach is that we don't need to instantiate objects to access some methods. For example, I have a class called Plane, in the pure Object Oriented philosophy that class (in data layer) should only contain methods for the object plane (e.g. getPlaneName(), setCodeNumber(), setPlaneName()) the class plane shouldn't implement methods to manipulate multiple planes like (getAllPlanes()) because the object plane only know himself.
In this pure object oriented approach we need to implement methods to work with multiple objects of the same class in a intermediate layer (e.g. in business logic layer or another if we work with 4 layers).

Despite of that, commonly we need to work with a relational database and we want that all "transactions" with the database remain in the Data Layer, so one common solution is the implementation of static methods in the Data layer classes (e.g. in class Plane implement the methods getAllPlanes() etc.).
In a conceptual way, we shouldn't need to instantiate a Plane to getAllPlanes, instead, the method getAllPlanes should retrieve an array with multiple Plane objects.

I hope I made it clear. This is the philosophy which I'm trying to implement using the CI. But in fact, maybe it would be much easier if I use non-static methods (but it doesn't make sense for me).

El Forum

[eluser]garymardell[/eluser]
I think what your trying to do is a little out of scope for the codeigniter framework. Really codeigniter is trying to make writing applications fast and more secure. Its probably not the "ideal" object orientated design that your going for. Codeigniter supports php4 aswell which means i would imagine alot of the features you are looking for and the way you expect them to work is not how they do work. It sounds like codeigniter framework isn't really what your looking for but rather a set of libraries to perform some of the tasks in your own "framework".I may be completely wrong.

I'm wondering when you have such a clear idea of object orientated principles why you are trying to use the codeigniter framework, if you really want something to work in a certain manner why not write it yourself.

El Forum

[eluser]Multisnet[/eluser]
We discussed a lot before start using the CI. Writing a "framework" is something very complex and it isn't in the plans of my company. Using CI should improve our work because it has a lot of features which made the implementation of an application much easier. This is our main idea. Honestly, we thought that it would be easy to use this common approach in CI.
For this project we must use CI (in fact we had a restrict deadline for project conclusion and we can't lose time). For future projects I'll talk with my colleagues and discuss this topic. For now I think the best solution is to make all methods as non-static, despite of losing some conceptual ideas it should work.

Thank you all for your opinions. I will post some more questions in the future. For sure!! Wink

El Forum

[eluser]Dam1an[/eluser]
I just skimmed this thread, so feel free to shout at me if I missed something, but you said you wanted to use static methods cause you don't want methods that manipulate multiple object in a model which is only aware of itself. So why not have 2 models, singular and plural? Such as User and Users etc
All the methods related to me (a single user) would go in the User model, and everything else (which you currently have as static) goes into the plural model.

El Forum

[eluser]n0xie[/eluser]
I understand the (textbook) theory behind using static methods: that was not the question.

The question was, why you would want to use them here (within Code Igniter).

CI uses 1 super object to which it binds all the other objects. This way you don't have to pass objects around, instead you can reference all your objects through the CI super object. You may want to read up on how Code Igniter works. Try this article: http://www.packtpub.com/article/codeigniter-and-objects.

Usually while writing software you want to keep the object instantiating to a minimum since they cost memory, but since PHP 'forgets' everything after parsing a script, there is really no cost penalty for instantiating every model (well obviously there is but it's minimal).

If you want a more modular approach you might want to take a look at the Zend framework.

Of course CI does allow you to call static methods but there really isn't a benefit I can think off (plus you lose all the references to the CI super object inside this scope). Just don't use the $this->load syntax but instead include your class and call it manually like you would do 'the normal way'.
Code:
include ('somepath/someclass.php');
someclass::somestaticmethod();

El Forum

[eluser]Multisnet[/eluser]
Dam1an:

Long time ago (4/5 years) I used the approach you talked about in some Java projects. There are a lot of developers who love that approach Wink
Sometimes, when we do things always in the same way, we narrow our view so much that we can't see other simple solutions. You gave me a great solution (easy implementation in CI and conceptually correct), I can't figure how I forgot that approach.

El Forum

[eluser]Multisnet[/eluser]
n0xie:

I tried that (using include and call a static method) but the problem, as you said, is that we lose all the references to the CI.
So, in your opinion I should use getLastNews() as a method of class New?
Pages: 1 2