How to include your own classes into models/controllers? |
[eluser]silverback[/eluser]
Hi, Lets say I have a class called Animal which I use to store data about animals. How do I use this class in my controllers/models? Do I make a new directory called classes, define it there, and then simply include('class.php') in my controllers and/or models? I guess you could also make it a model, but then the problem becomes that you can only have one instance of it per controller/model (right?). What if I want an array of Animal objects? Thanks
[eluser]skunkbad[/eluser]
[quote author="silverback" date="1270410120"]Hi, Lets say I have a class called Animal which I use to store data about animals. How do I use this class in my controllers/models? Do I make a new directory called classes, define it there, and then simply include('class.php') in my controllers and/or models? I guess you could also make it a model, but then the problem becomes that you can only have one instance of it per controller/model (right?). What if I want an array of Animal objects? Thanks[/quote] You could watch this video: http://net.tutsplus.com/tutorials/php/co...+(NETTUTS) and you will know everything you need to know. Well, I don't know about your last question... I'd assume you would have to create a new object for each instance of the class.
[eluser]silverback[/eluser]
Thanks for the link, it was a great video. Unfortunately this still doesnt solve my problem. How would I work with many animals? I can create an Animal library now, but I can't load more than once instance per controller right?
[eluser]danmontgomery[/eluser]
Include your class file at the top of the controller file, then: Code: $this->giraffe = new Animal(); or, Code: $giraffe = new Animal(); |
Welcome Guest, Not a member yet? Register Sign In |