Garethp Posted June 17, 2010 Share Posted June 17, 2010 So I had this idea for my classes when I was going to sleep to make it easier to edit and add to them. I really want to have my functions in a class over multiple files. I would like something like this <?php if(!isset($_GET['class'])) { exit(); } $class = $_GET['class']; class $class { include "$class/Login.php"; include "$class/Logout.php"; } ?> But with a glob(); on the directory. However when I tried something like that as a test, it didn't work. Can someone give me an example of how I would do this? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/ Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 I don't think this is possible. Why would you even want to do it? What added benefit are you seeking? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073295 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 You can't do this. And in all seriousness, your classes shouldn't really be that large that they would ever need it. Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073299 Share on other sites More sharing options...
Garethp Posted June 17, 2010 Author Share Posted June 17, 2010 I just like the idea behind it. Make my code more readable and accessible. Doesn't have anything to do with size. My User class for instance only has about 10 functions (for now) (I think). So there's no way to do it? Not even have, say, a chain of extends that has the extension class names for the previous class defined by a variable? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073309 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 So there's no way to do it? None that would be particularly efficient or well designed, its not part of the language. The classkit extension may be of use, but it will make your scripts unportable across servers. Not even have, say, a chain of extends that has the extension class names for the previous class defined by a variable? That sounds like a terrible idea. Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073311 Share on other sites More sharing options...
salathe Posted June 17, 2010 Share Posted June 17, 2010 It sounds like you're wanting to do too much with a single class. Can you give a concrete example of a class that you have which you think would benefit from being spread across multiple files, and explain why you think so? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073317 Share on other sites More sharing options...
Garethp Posted June 17, 2010 Author Share Posted June 17, 2010 Honestly? I think a user management class would benefit from it. My user management class has the following functions Restrict Login Get Info Edit User Calculate Age __construct And it already spans 400 lines. And ultimately I also want it to have Send PM Get PMs Favorite Favorites Follow Add Contact Submit Story Forum Login Chat Login And many more. As it is it's a pain in the ass to go over my functions when I think that they need adjusting, and I figure it would be so much easier to read, edit and add functions if they were all in separate files under one folder called "User". I really dislike having a script as long as it is, especially when it could get MUCH longer Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073319 Share on other sites More sharing options...
salathe Posted June 17, 2010 Share Posted June 17, 2010 It looks like you're not really grasping the concept of OOP clearly at all, instead you're essentially wrapping a bunch of procedural behaviour (discrete functions) within a class. Those different parts that you mention should belong in very different, separate classes; just to give a couple of examples, logging in is all about authentication whereas restriction is about authorization -- two separate entities right there! Getting/sending PMs is entirely unrelated to retrieving a user's age, so should definitely not belong in the same class. Etc.. Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073322 Share on other sites More sharing options...
Garethp Posted June 17, 2010 Author Share Posted June 17, 2010 I just thought that OOP was a convenient way to store and use functions that relate to a certain element. What's the concept of OOP then? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073333 Share on other sites More sharing options...
trq Posted June 17, 2010 Share Posted June 17, 2010 I just thought that OOP was a convenient way to store and use functions that relate to a certain element. What's the concept of OOP then? A class defines the characteristics and behaviors of a single thing (an object). You are inadvertently jamming unrelated functionality within this object. OOP is not something that is going to be explained in a single post, maybe you should invest in a good book on the subject? Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073334 Share on other sites More sharing options...
Garethp Posted June 18, 2010 Author Share Posted June 18, 2010 Ok, I know you guys said it was a bad idea, but I was too curious, because I knew there had to be a way to do it, and there was. Here' what I have <?php class User { public function __call($method, $args) { if (isset($this->$method) === true) { if(isset($args[0])) { $args = $args[0]; $func = $this->$method; $func($args); } else { $func = $this->$method; $func(); } } } } ?> And so I can use $User->Login = function() { //stuff } And even have one parameter. That's all cool and works. But when I have ($param1, $param2) I have no idea how to make it work. I usually use associative arrays instead of set parameters anyway, so it's no big deal, but I'm just curious as to how I would get it to work Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073790 Share on other sites More sharing options...
Mchl Posted June 18, 2010 Share Posted June 18, 2010 If you really wish to go in this direction, you can use call_user_func_array This undoubtly an interesting approach and I'm sure you've got a lot of fun developing it. Just be aware that it's breaking many of well established rules of OOP like: http://en.wikipedia.org/wiki/Single_responsibility_principle http://en.wikipedia.org/wiki/Interface_segregation_principle http://en.wikipedia.org/wiki/Don%27t_repeat_yourself Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073791 Share on other sites More sharing options...
trq Posted June 18, 2010 Share Posted June 18, 2010 I'm not sure it can be breaking that many rules as languages that are better suited to OOP then php (eg Ruby and Python) allow similar approaches within there design. PHP however doesn't, and I'm really not that sure the approach you are taking with php is particularly well thought out, designed, or what that feature was provided to actually do. Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073840 Share on other sites More sharing options...
Mchl Posted June 18, 2010 Share Posted June 18, 2010 I was referring more to the idea of cramming as much functionality as possible into a single class (that's how I understood OP's wishes) Quote Link to comment https://forums.phpfreaks.com/topic/205020-single-class-across-multiple-files/#findComment-1073923 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.