genetheblue Posted August 6, 2007 Share Posted August 6, 2007 Hi, I've just started to use php, and haven't worked with any web server language in years. I found I can create classes and objects and make a php 'application' structured how I am used to(actionscript & c#). Is this a bad approach? Is there any downsides/overhead to making everything a class that I possibly can? It makes more sense to my brain to write code that way - but I am being questioned if it a good say to structure php. Just for instance, for a simple user management... I have a mainpage class that outputs all the basic html, a manageusers class that extends mainpage to handle all the specific forms and behaviors, and a database class to handle all the database calls. Am I heading in an 'acceptable' path, or should I rethink what I'm doing and write more 'inline' php? Sorry if I'm being vague, just starting out and having a hard time describing my questions running thorugh my head. Any comments would be appreciated! Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/63596-just-started-using-php-question-about-stucture/ Share on other sites More sharing options...
phpknight Posted August 6, 2007 Share Posted August 6, 2007 Yes, you have the right idea. Since PHP 5, it is not a bad idea to do OOP. If you have version 4.x, upgrade to 5.2.3 because the class support is much better. There is overhead involved. IMHO, you just have to be careful how you initialize an object since HTML is stateless. You can end up doing something over and over again that you would just do once in C++. So, you do not want to load too much information in there like you would probably want to with a C++ app. You can use sessions and stuff, but it still is not the same. Quote Link to comment https://forums.phpfreaks.com/topic/63596-just-started-using-php-question-about-stucture/#findComment-317002 Share on other sites More sharing options...
genetheblue Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks very much, I'm sure I'll answer alot of the questions being imposed on me as I learn and become more familiar with php. I do have another question though, and maybe I should start another topic (I see there is an OOP subforum, sorry if I'm in the wrong place... feel free to tell me to get out of here ). If I want to destroy an instance of an object, what is the surest way of doing that? From my reading, I'm seeing using unset($objRef, 'null') to get rid of it. Garbage collection would come along, and the built in __desonstruct would fire. Just the nature of webpages, am I right that there is no real way for me to 'see' this was called? If I throw a echo in there - I'm never going to see it, unlike a client application where I could put debug code in the destroy method to see that it is actually being called? Quote Link to comment https://forums.phpfreaks.com/topic/63596-just-started-using-php-question-about-stucture/#findComment-317855 Share on other sites More sharing options...
phpknight Posted August 8, 2007 Share Posted August 8, 2007 There are ways to do that, but for the most part, I would just let garbage collection handle it--that is the beauty of it in comparison with C++. Quote Link to comment https://forums.phpfreaks.com/topic/63596-just-started-using-php-question-about-stucture/#findComment-318041 Share on other sites More sharing options...
btherl Posted August 8, 2007 Share Posted August 8, 2007 A side note here - if an object is in scope (which includes all objects at the top level), then it will not be garbage collected automatically. It must be unset if you want to recover memory for use later in the current request. But if it's gone out of scope (for example, it was created inside a function and no references escaped to outside the function) then it will be GC'd automatically. All data is freed at the end of the request. Sessions are serialized and stored on disk for the next request to find. I can't answer your question about echoing within a destructor, sorry. But I expect it would be possible, for debugging purposes. Quote Link to comment https://forums.phpfreaks.com/topic/63596-just-started-using-php-question-about-stucture/#findComment-318054 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.