LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2008, 12:18 PM   #1
konqi
Member
 
Registered: Oct 2007
Distribution: Gentoo, Slackware
Posts: 162

Rep: Reputation: 16
PHP: why OOP over normal functions?


what is the advantage of OOP over normal functions?

Why should i use OOP?
 
Old 04-29-2008, 12:31 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
OOP let's you organize the code better. It's easier to maintain a complex code when it's organized in classes. It's easier to understand it when it's object oriented as well as reuse objects across different applications. If the code is designed well and according to OO design principals it might be really helpful to use OOP. However in PHP, IMHO, introduction of first form of OOP leaded many developers to creating the code that did not comply to the OOP standards. Many people started using objects without even understanding what encapsulation or inheritance is... therefore so much crappy code has been created. But, people learn on their own mistakes - don't they?
 
Old 04-29-2008, 05:54 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Essentially OOP is a design decision.

In procedural programming you will define you data structures and then design the functions that will manipulate these data structures. The two are quite separate entities and it is possible to have functions that will operate differently depending on which data structure is passed in, meaning that the function is almost independent of the data structure.

In Object Orientated programming the data and the functions are tightly coupled. That is a class will define the data and the methods together.

Both approaches work, it's just that the the OOP paradigm scales better. So the industry has gone a little OO crazy. But there is still no substitute for good design.
 
Old 04-29-2008, 06:15 PM   #4
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
Quote:
Many people started using objects without even understanding what encapsulation or inheritance is... therefore so much crappy code has been created. But, people learn on their own mistakes - don't they?
I am one of those people. The code works, but very inefficient and the way it grew, it's all in a mess so much of a mess (specially repetition of code) that's it becoming hard to maintain my scripts, and I tend to make it even more messier as I add new functionality.
 
Old 04-30-2008, 06:29 AM   #5
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
I'd say try to redesign the code... if it's not to late And yeah, repetitions suck big time. One of the main points of using OOP is to avoid creating them. But as you can see, without a proper design skills OO program can be more difficult to read than a bunch of well organized functions, so people should think twice before using these practices. And maybe learn UML before doing anything?
Quote:
So the industry has gone a little OO crazy
Maybe even TOO OO crazy

Last edited by krizzz; 04-30-2008 at 06:31 AM.
 
Old 05-08-2008, 08:47 AM   #6
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
Procedural code is faster
 
Old 05-08-2008, 08:55 AM   #7
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
IMNSHO, OOP should be used sparingly and only in certain specific circumstances in scripts, in interpreted languages such as PHP, and in "compiled on the fly" languages such as perl. OOP increases server load in such cases (sometimes it hugely increases server loads), and the fact that hardware is now generally faster than software doesn't excuse sloppy programming that wastes huge numbers of bus cycles.

On the other hand, in a compiled language OOP can make all kinds of sense because properly designed objects do indeed help a project scale and can indeed greatly enhance readability.
 
Old 05-08-2008, 09:21 AM   #8
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
I totally agree with you.
 
Old 05-08-2008, 10:05 AM   #9
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
In my opinion programs should always be developed to be efficient, as simple as possible and fit the job at hand rather than follow some universally cool way of making software.

Sure you can write 'cat' in object-oriented style, but it's probably not worth it..
 
Old 05-08-2008, 10:51 AM   #10
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
Wink

Useful seminar which discusses the pros and cons of OOP versus procedural

http://www.youtube.com/watch?v=JTqVJa07NVQ
 
Old 05-08-2008, 03:32 PM   #11
seraphim172
Member
 
Registered: May 2008
Posts: 101

Rep: Reputation: 15
OOP better for complex projects

When working on many different projects where similar functionality is required, and all that under deadline test, then OOP code can make it significantly easier to proceed with the job without having to remember all intricate details.

Work without OOP becomes difficult as the project grows in size and time is running out for delivery dates. This is even true for those who do not like OOP constructs.

Linux Archive

Last edited by seraphim172; 06-25-2008 at 04:22 AM.
 
Old 05-09-2008, 06:38 PM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I would say that there is a role for OO in web development using scripting languages (such as PHP). For someone who is comfortable with OO concepts they can produce well designed code. Yes there is an overhead but that may not be significant, the extra safeguards that OOP provides can lead to savings in the development and maintenance. So long as those savings are greater that the cost of the extra work the hardware is requested to do it is a viable solution.
 
Old 05-09-2008, 08:41 PM   #13
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
And someone who is comfortable with procedural concepts can produce well designed code in a scripting language without the overhead of OO.
 
Old 05-09-2008, 09:01 PM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Agreed, but it is generally easier to structure large application using O-O design. Which is why many applications, even small ones are done using OO concepts because it has been decided by the developers that an O-O project will be easier to maintain and that it will scale better. That is not to say that there is no scope for pure procedural (or even a mixture) but O-O does have some advantages which tend to be when it comes to maintaining and enhancing the application.
 
Old 05-10-2008, 11:07 AM   #15
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
OO is the way to go in a project that is expected to become large and an environment that is compiled.

But employing OO by itself is no guarantee of maintainable code. Some of the most impenetrable code I have EVER tackled was written in C++ and was designed to solve Schroedinger's Equation and Poisson's equation in a layered semiconductor structure. It was fully object-oriented, including in some instances where that added a great deal of complexity while not adding any particular benefit. Variables were named somewhat arbitrarily - and in a couple of cases, deceptively. The documentation was spotty.

I spent a LONG TIME reverse engineering that code to learn WTF was going on. Many aspects of the particular problem would have made a lot more sense if written procedurally, and of course the thing was just badly designed anyway.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php time bomb with php native functions secretlydead Programming 3 02-01-2008 06:10 PM
Does OOP make PHP slower? simopal6 Programming 2 04-20-2007 10:13 PM
OOP in PHP patpawlowski Programming 5 11-20-2005 04:37 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration