[PHP] CodeIgniter style $this->modelname->function() calls
hi, what i'm trying to do is access models in the codeigniter style (while using my own mini-mvc boilerplate type thing), by calling $this->model_name->function() in my controllers. heres what I've done so far:
PHP Code:
$this->load = new Load();
foreach (glob("application/models/*.php") as $file) {
$model = basename($file, ".php");
$this->$model = new $model;
}
I know I can't do what I tried there, but hopefully you can see my objective. What I want to do is make php write $this->modelname = new modelname; for each file in the model folder. I'm grabbing all the php files and stripping the directory and .php to leave just the file name.
I apologise if this is hard to understand, its hard to explain >.<
Thanks in advance :D:
Re: [PHP] CodeIgniter style $this->modelname->function() calls
im doing research into this too and i also cant figure it out (yet)
its called method chaining and im studying the vbulletin code to understand this chaining process (kinda hard LOL).
Re: [PHP] CodeIgniter style $this->modelname->function() calls
Wait, you're loading each class from a given folder to a variable? Why not instantiate the object into an array element;
like:
PHP Code:
class cls_name
{
protected $model = Array();
//...
foreach( glob("application/models/*.php") as $file )
{
$model = basename( $file, '.php' );
$this->model[ $model ] = new $model();
}
it'll be easier to use thereafter.
And what happens when you try what you had?
Re: [PHP] CodeIgniter style $this->modelname->function() calls
nevermind, i figured it out. for some reason I have to load each file in the foreach loop rather than elsewhere in the file O.o
If i do it like this it works fine:
PHP Code:
foreach (glob("application/models/*.php") as $file) {
@include_once $file;
$model = basename($file, ".php");
if (class_exists($model)) {
$this->$model = new $model;
} else {
//handle error, trow an exception?
}
}
also improved the code slightly.
holt, if you want to see how its done overall I can release my 'boilerplate' php setup if it would help you. its just a really small basic mvc 'framework', for times when I feel like I don't want to write shit code, but codeigniter is too heavy for a small project
Re: [PHP] CodeIgniter style $this->modelname->function() calls
im at work right now but at home i have a script that loads each class in a foreach already.
i also use php singleton
Re: [PHP] CodeIgniter style $this->modelname->function() calls
I use lazy loading so I don't gotta include_once
Re: [PHP] CodeIgniter style $this->modelname->function() calls
oh cool, this helped me alot thanks.
Re: [PHP] CodeIgniter style $this->modelname->function() calls
Quote:
Originally Posted by
Putako
oh cool, this helped me alot thanks.
You're using CI now?
Aaron
Re: [PHP] CodeIgniter style $this->modelname->function() calls
Code integer is pwn. In conclusion, yes :D yes i am.
Re: [PHP] CodeIgniter style $this->modelname->function() calls
if this helped you alot then you are not using coedigniter.
you are a huge fucking faggot.
Re: [PHP] CodeIgniter style $this->modelname->function() calls
Quote:
Originally Posted by
Putako
Code integer is pwn. In conclusion, yes :D yes i am.
you can't even spell it right dude..
Re: [PHP] CodeIgniter style $this->modelname->function() calls
You could just use an autoloader instead... also (sorry if I do not understand your intentions), if you would like a more modular framework, I would suggest you go with Kohana 3 - a fork of CI - which takes on a (H)MVC approach. Otherwise, just download the Modular Extensions (H)MVC for CI. Then, you can cross-link, cross-load and cross-sex (serious business) between modules; and extend your controllers and models without the need to include them.
Re: [PHP] CodeIgniter style $this->modelname->function() calls
the point is that I'm only using this for really basic small little mostly static projects.
for anything where I can be assed I generally use CI.
looking into FUEL also, haven't actually tried it yet.
Re: [PHP] CodeIgniter style $this->modelname->function() calls
Quote:
Originally Posted by
Zen
the point is that I'm only using this for really basic small little mostly static projects.
for anything where I can be assed I generally use CI.
looking into FUEL also, haven't actually tried it yet.
CodeIgniter for static and small projects? I don't think so. Once you introduce a framework into your project, it is no longer 'static' projects as you would have introduced some elements of dynamic content. I prefer CodeIgniter for intermediate-scale projects, however, nowadays, I utilise Kohana 3 in majority of my small to intermediate-scale projects.
If you would like my advise on a PHP framework for small scale projects, I suggest you have a look at the PHP Fat-Free Framework (F3). Indeed, its file size is only 55KB 'large' and it is comprehensive enough to cover the necessities of a small scale project. I'm pretty surprised it has not gain much recognition yet, but once again, have a look at it. It is definitely worth trying out other frameworks rather than sticking solely on CI.
Also, you can have a look at LightVC if you are looking to create projects leaning towards 'static content' more than 'dynamic content.' As evident from the name of the framework, it does not include any models. It only consist of a view and a controller which is perfect for setting up small-scale websites that do not require any database connection. Of course, if your project does rely on a database, simply download an external ORM library - e.g. Doctrine, Propel or CoughPHP (LightVC developers). The great thing about LightVC is that it provides a lot of room for you to modify and expand - in short, personalise.
Most importantly, follow what you prefer most and what you are most comfortable with - to ensure optimum productivity :)