Custom classes in CodeIgniter

前端 未结 6 566
走了就别回头了
走了就别回头了 2021-01-31 05:45

Seems like this is a very common problem for beginners with CodeIgniter, but none of the solutions I\'ve found so far seems very relevant to my problem. Like the topic says I\'m

6条回答
  •  难免孤独
    2021-01-31 06:10

    CodeIgniter doesn't really support real Objects. All the libraries, models and such, are like Singletons.

    There are 2 ways to go, without changing the CodeIgniter structure.

    1. Just include the file which contains the class, and generate it.

    2. Use the load->library or load_class() method, and just create new objects. The downside of this, is that it will always generate 1 extra object, that you just don't need. But eventually the load methods will also include the file.

    Another possibility, which will require some extra work, is to make a User_Factory library. You can then just add the object on the bottom of the file, and create new instances of it from the factory.

    I'm a big fan of the Factory pattern myself, but it's a decision you have to make yourself.

    I hope this helped you, if you have any questions that are more related to the implementation, just let me/us know.

提交回复
热议问题