scrubbicus Posted August 11, 2009 Share Posted August 11, 2009 So I have a users class that I want to call within my data class. What's the best way to do this? Currently I have, and it works. public function __construct() { $this->_users = new users; } then I can call it as needed from within the class but now that class is being called TWICE so anything I have within the constructer user will be done twice. Is there any other ways I can use a class within another class? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 11, 2009 Share Posted August 11, 2009 pass it? <?php public function __construct($usersClass) { $this->_users = $usersClass; } ?> I'm not sure I understand your question. Could you elaborate on what you're trying to accomplish? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 11, 2009 Share Posted August 11, 2009 You could make Users a singleton. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 11, 2009 Share Posted August 11, 2009 You could make Users a singleton. I much prefer dependency injection to singletons, they're much too global and make unit testing a hassle. Of course singletons are good in some circumstances, but I try to avoid it if possible. Quote Link to comment Share on other sites More sharing options...
scrubbicus Posted August 12, 2009 Author Share Posted August 12, 2009 Like for instance if you had two classes and you had a function that was in one of the classes that you wanted to use in the other class. What would be all the ways, or the best way, to accomplish creating a new instance of that class and calling it from within that class. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted August 12, 2009 Share Posted August 12, 2009 Long version: Well it depends, are you programming like an object oriented programmer? If so, you should be creating objects and performing actions ON those objects, not just calling methods IN them. At that point it should be clear whether you want to create a new copy of an object or pass the routine an existing one and you probably wouldn't need to be asking us this question. If all you're doing is taking your procedural program and putting it inside of classes, then you're not programming like an object oriented programmer and it would be less confusing to just make the classes/methods static so you don't have to instantiate them to begin with. Once you reach that point, you might just raise the question of "why don't I just program procedurally to begin with?" Short version: If you find yourself creating new classes just to use the methods inside of them and not because you're trying to represent a new "user" or whatever it is your class is supposed to represent, it should probably be a static method. Or, more likely, you're not designing the program in a proper object-oriented manner to begin with. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.