Jump to content

Recommended Posts

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...

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.

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 :P ).

 

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.