Welcome Guest, Not a member yet? Register   Sign In
Make own objects
#1

[eluser]juan1904[/eluser]
Hi,

I'm making a playoff schedule for a PS3 EA Sports NHL09 league.
I'm going to make an own class called Game something like this:
Code:
class Game {
  $hometeam;
  $awayteam;

  function __construct($home, away) {
    this.$hometeam = $home;
    this.$awayteam = $away;
  }

  // and some more code here!
}

Then in my controller or in a model I'm gonna make new objects out of this class like $game1 = new Game("Red Wings", "Penguins");

I wonder where to put my new class in codeigniter framework?
#2

[eluser]TheFuzzy0ne[/eluser]
./system/application/libraries/game.php

Then load it like this:
Code:
$this->load->library('game');

Then access it like this:
Code:
$this->game->get_scores();

You can also auto load the library on each request if you so desire.
#3

[eluser]Dam1an[/eluser]
What you could do, is create a seperate folder for all your objects, then you can just create an autoload function which will load them from that directory, which saves you doing the includes manually (of course if you only have one or 2 objects like this used in one or 2 places, it's not a bit deal, but if you end up with loads, it'll make life that little bit easier)

(Autoload only work with PHP5+, which I assume you are, as you're using the PHP5 constructor syntax)
#4

[eluser]juan1904[/eluser]
Thank you TheFuzzyOne and Dam1en for very fast answers! How can I autoload a folder of libraries? I've only autoloaded single libraries.
#5

[eluser]Dam1an[/eluser]
It doesn't load the entire folder as you think
it loads them on demand, so when you create an object which it doesn't know, cause you've not included it, it will execute the __autoload function, which finds and loads it

Read up more about it here
#6

[eluser]juan1904[/eluser]
Alright, and that function should be implemented in my MY_Controller?
#7

[eluser]Dam1an[/eluser]
tbh, you can put it pretty much anywhere that will get executed before you crate any of your custom objects, so yes, MY_Controller would be a suitable place
#8

[eluser]juan1904[/eluser]
Hm the autoload function doesn't seem to work for me, it looks like this now:
Code:
function __autoload($class) {
        require_once 'system/application/libraries/objects/' . $class . '.php';
    }

And I get this error message:

Fatal error: Class 'Game_serie' not found in <path to my controller here> on line 14.

This controller file extends MY_Controller aswell.
#9

[eluser]TheFuzzy0ne[/eluser]
[quote author="juan1904" date="1244944151"]Fatal error: Class 'Game_serie' not found in <path to my controller here> on line 14.[/quote]

Look harder! Wink
#10

[eluser]juan1904[/eluser]
Sorry I can't really see the problem here. The __autoload function should include "system/application/libraries/objects/Game_serie.php" but it doesn't.




Theme © iAndrew 2016 - Forum software by © MyBB