<p>I am writing a webapp using CodeIgniter that requires authentication. I created a model which handles all my authentication. However, I can't find a way to access this authentication model from inside another model. Is there a way to access a model from inside another mode, or a better way to handle authentication inside CodeIgniter?</p>

[{'answer_id': 46356, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 5, 'selected': True, 'text': "<p>In general, you don't want to create objects inside an object. That's a bad habit, instead, write a clear API and inject a model into your model.</p>\n\n<pre><code>&lt;?php\n// in your controller\n$model1 = new Model1();\n$model2 = new Model2();\n$model2-&gt;setWhatever($model1);\n?&gt;\n</code></pre>\n"}, {'answer_id': 74972, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Don't handle authentication in your model. Only use models to interface with your database, or ldap or whatever.</p>\n\n<p>I created an Auth library that I use to manage authentication and authorization. You can access a library like this from your controllers.</p>\n"}, {'answer_id': 75087, 'author': 'Christian Davén', 'author_id': 12534, 'author_profile': 'https://Stackoverflow.com/users/12534', 'pm_score': 4, 'selected': False, 'text': '<p>It seems you can load models inside models, although you probably should solve this another way. See <a href="http://codeigniter.com/forums/viewthread/49625" rel="noreferrer">CodeIgniter forums</a> for a discussion.</p>\n\n<pre><code>class SomeModel extends Model\n{\n  function doSomething($foo)\n  {\n    $CI =&amp; get_instance();\n    $CI-&gt;load-&gt;model(\'SomeOtherModel\',\'NiceName\',true);\n\n    // use $CI instead of $this to query the other models\n    $CI-&gt;NiceName-&gt;doSomethingElse();\n  }\n}\n</code></pre>\n\n<p>Also, I don\'t understand what Till is saying about that you shouldn\'t create objects inside objects. Of course you should! Sending objects as arguments looks much less clear to me.</p>\n'}, {'answer_id': 17734189, 'author': 'saada', 'author_id': 1204900, 'author_profile': 'https://Stackoverflow.com/users/1204900', 'pm_score': 1, 'selected': False, 'text': '<p>Loading a model within a model is now possible with the new CodeIgniter.</p>\n'}]
['https://Stackoverflow.com/questions/46338', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/889/']