Jump to content

Why use OOP?


SyncViews

Recommended Posts

I really can't see a reason to use OOP. Seeing as a php script runs just once I can't see a purpose of objects (eg if I was useing c++ and making CnC I would use objects for say buildings as there is more than one and each needs it's own vars)

 

Why bother useing OOP for say bbcode when I could just a have a function that takes a string, turns the BB code into html then echos it.

 

I'm sure there is a reason for useing OOP for these things but I reeally can't see any use in php...

Link to comment
Share on other sites

It's down to preference really. I never used to use OOP but since I've programmed more and more in PHP I've found uses for it. It's nice to have certain things all contained in state-aware functions. Also, objects can technically persist over sessions.

 

You don't have to use OOP though. Some people just prefer procedural programming while others see things interacting as objects.

 

If what you're doing now works for you then don't worry about OOP.

Link to comment
Share on other sites

personally... i see no uses for server sided OOP... in client sided i can see all kinds of uses... autoformatting, timers, spawners(games), etc... but on client side, your always running top to bottom(no timers) and all formatting can be done by flatfile functions...

Link to comment
Share on other sites

But you can serialize objects and have them persist over sessions. Also, like Liquid Fire said, OOP has lots of benefits in terms of data structure and interaction. It's also much better for collaborative projects as it provides encapsulation.

Link to comment
Share on other sites

I still don't really see a use of it in php.

 

In c++ I used oop for stuff like making an ini praser as I want to have multiple files open at once. But then thats because I need to keep the ini file open between loops so that the data is updated all the time before being saved on exit. so "start program -> prase files(like 10) -> START LOOP -> get user input -> update ini data strutures -> CONTIUE TO LOOP UNTILL EXIT -> save files -> exit"

 

Php doesn't loop like that as user input requires a new page so it ends up as "prase file A-> echo data -> prase file B -> send page to client " then the next page when the user presses submit "build fileA-> save file -> build fileB -> save file -> end"

Link to comment
Share on other sites

OOP is seeming to be more of a standard for larger projects, because its easier to manage and cuts down a little bit on code from functions which in a large project adds up. Anything can be done in anyway, its just on average OOP is more efficient.

Link to comment
Share on other sites

  • 2 weeks later...

OOP is also good to group functions that the are common to each other a perform actions on the same from of data.

 

I'm a newbie in OOP but it's always a question in my mind. It sounds great to have a group of functions together. But what about the optimized coding and RAM usage? I mean when we include a large class file to a project, there may be some unnecessary methods depending on the project. Isn't it a bad programming practice?

Link to comment
Share on other sites

OOP is also good to group functions that the are common to each other a perform actions on the same from of data.

 

I'm a newbie in OOP but it's always a question in my mind. It sounds great to have a group of functions together. But what about the optimized coding and RAM usage? I mean when we include a large class file to a project, there may be some unnecessary methods depending on the project. Isn't it a bad programming practice?

 

The only way to make sure the functions you include for a page are all needed it to have each function is a seperate and then include each file that has a function you need, That does not seems optimized.  How would you optimized it?

Link to comment
Share on other sites

can someone show me a simple diffrence between code written in PHP and in OOP? and an advanced code written in PHP and OOP i wanna see the diffrence between them.

 

thanks.

 

I think you mean procedural and OOP.  I could show you an example of a php framework that i built OO Programming but i would never waste the time building is in purely procedural.  showing you simple examples is not going to work because simple example looks like procedural is just as good but as thing grow you then begin to see the issues or maintain and scalability.

Link to comment
Share on other sites

OOP is also good to group functions that the are common to each other a perform actions on the same from of data.

 

I'm a newbie in OOP but it's always a question in my mind. It sounds great to have a group of functions together. But what about the optimized coding and RAM usage? I mean when we include a large class file to a project, there may be some unnecessary methods depending on the project. Isn't it a bad programming practice?

 

There's a couple things that help here.  You can have php autoload a file containing your class when it is called upon, otherwise it will not be loaded.  Also, you can have one generic class with a lot of functionality, then you can have smaller more specific classes that inherit the generic classes methods/variables, then customize the it.  So basically you can write one class, and stem from it many classes without rewriting all the code per class. 

 

You can look at it like a mold.  Say you have a carver that's starts from a square block and they need to carve 10 human figures.  They could use a mold (assuming they're carving plastic for some reason) to start with a generic human shape and then go from there, or they can just start with the square block and waste time.  (Maybe not the greatest analogy every thought of but I think it works)

Link to comment
Share on other sites

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.