rockinaway Posted March 21, 2007 Share Posted March 21, 2007 How would I call another class from a different file, into my function? Would I have to require and then do new class()? Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/ Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <?php include('classfile.php'); function createClass(){ return new class(); } ?> I think that should work, but not sure. Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-212129 Share on other sites More sharing options...
rockinaway Posted March 21, 2007 Author Share Posted March 21, 2007 That didn't work.. but I figured it out... just needed a global call Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-212133 Share on other sites More sharing options...
Jenk Posted March 30, 2007 Share Posted March 30, 2007 "global" is a code smell. Pass your objects and variables around, as they should be handled. Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-218018 Share on other sites More sharing options...
rockinaway Posted March 30, 2007 Author Share Posted March 30, 2007 No you see, I am wroking in a software and everything is handled properly.. is is how it has been done ... Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-218234 Share on other sites More sharing options...
emehrkay Posted March 31, 2007 Share Posted March 31, 2007 include('path_to.class.php'); //class name is class{} function example(){ $class = new class(); } Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-218466 Share on other sites More sharing options...
Jenk Posted March 31, 2007 Share Posted March 31, 2007 No you see, I am wroking in a software and everything is handled properly.. is is how it has been done ... It's still a code smell. Negates the entire reason for having a modular application, but it breaks the capsulation by having a direct and explicit dependency on not only a global value, but also the name of the variable containing that value. It is much better practice to present that value as a parameter to your object. Link to comment https://forums.phpfreaks.com/topic/43697-calling-a-class-from-another-file-in-a-function/#findComment-218806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.