Page 1 of 1
Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 3:48 pm
by swraman
Hi,
This may sound dumb, but now that I am writing scripts for other people I want to know:
Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?
up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?
Thanks
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 4:00 pm
by Benjamin
The best way to put this, from my perspective, is that the functions really aren't doing everything you need them to do, you just think they are.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 4:51 pm
by Theory?
I love the warring factions mindset. Balance is the key to true zen.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 5:04 pm
by Christopher
swraman wrote:Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?
up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?
I think your attitude shows part of the problem with discussing methodologies. Do you want do look good or do you want to produce well designed and maintainable applications.
And I want to be clear that in PHP you still program procedurally when you are programming using OOP. Vice versa that is not the case.
If you are not using OOP then you are simply choosing to not use some powerful language constructs that exist in PHP. So if you are choosing not to use certain parts of the language (in this case 'class' but it could be 'switch', 'foreach', etc.) then you have to ask yourself why you are choosing not to use them. Do they lead to bad practices like 'goto' for example? Do you not understand how to use them?
There is nothing inherently good or bad about any of these language constructs. Each is useful in certain circumstances.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 5:07 pm
by FunkyDude
I've been reading/learning OOP, and from what I'm understanding, its more about organization, or making your code easier to reuse, extend, or share with others for them to improve on. Writing a script in OOP or procedurally can give the same end result.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 6:45 pm
by swraman
arborint wrote:swraman wrote:Is OOP suposed to be "classier" or cleaner than regular old procedural programming? Is is suposed to be better to write PHP with objects?
up till now I have only been doing procedural programming, functions seem to do everything I need them to do. But would the code "look better" in the professional's eye if it was done with object oriented programming instead?
I think your attitude shows part of the problem with discussing methodologies. Do you want do look good or do you want to produce well designed and maintainable applications.
And I want to be clear that in PHP you still program procedurally when you are programming using OOP. Vice versa that is not the case.
If you are not using OOP then you are simply choosing to not use some powerful language constructs that exist in PHP. So if you are choosing not to use certain parts of the language (in this case 'class' but it could be 'switch', 'foreach', etc.) then you have to ask yourself why you are choosing not to use them. Do they lead to bad practices like 'goto' for example? Do you not understand how to use them?
There is nothing inherently good or bad about any of these language constructs. Each is useful in certain circumstances.
I understand how to use them, but I've never actually programmed anything using OOP techniques. But now on a project im working on I am noticing a lot of places that I can use objects to replace the code I currently have, and it would probably make the code look a bit cleaner. Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 7:38 pm
by swraman
astions wrote:The best way to put this, from my perspective, is that the functions really aren't doing everything you need them to do, you just think they are.
I believe you, but I honestly dont know where OOP can do something that procedural can't...
Is there any way to have an input arguement to a class?
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 8:01 pm
by Benjamin
Here is a simple example of a class that represents an object:
Code: Select all
class number {
private $_n;
public function __construct($n = 0, $base = 10) {
$this->_store($n, $base);
}
public function set($n, $base = 10) {
$this->_store($n, $base);
}
public function get($base = 10) {
return $this->_retrieve($base);
}
private function _store($n, $base) {
switch ($base) {
case 2:
$this->_n = bindec($n);
break;
case 8:
$this->_n = octdec($n);
break;
case 10:
$this->_n = $n;
break;
case 16:
$this->_n = dexhec($n);
break;
default:
$this->_n = $n;
trigger_error("" . get_class($this) . "() Second parameter must be one of 2, 8, 10, 16");
break;
}
}
private function _retrieve($base) {
switch ($base) {
case 2:
return decbin($this->_n);
break;
case 8:
return decoct($this->_n);
break;
case 10:
return $this->_n;
break;
case 16:
return hexdec($this->_n);
break;
default:
trigger_error("" . get_class($this) . "() Second parameter must be one of 2, 8, 10, 16");
return $this->_n;
break;
}
}
}
And it's usage:
Code: Select all
$n = new number(50);
echo $n->get(2) . "<br />";
echo $n->get(8) . "<br />";
echo $n->get(10) . "<br />";
echo $n->get(16) . "<br />";
Now, you can pass this object to other objects that can retrieve it's value in the format they need it in, without having to worry about doing it yourself.
This would allow you to create a calculator class that could perform math on a number regardless of it's base. This is just an example though, it would only actually work on real numbers.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 8:24 pm
by VirtuosiMedia
I wrote a fairly lengthy post on another forum about
OOP vs. Procedural programming. You might find it useful.
Re: Is Procedural looked down upon compared to OOP?
Posted: Mon Mar 09, 2009 9:29 pm
by allspiritseve
swraman wrote:I understand how to use them, but I've never actually programmed anything using OOP techniques. But now on a project im working on I am noticing a lot of places that I can use objects to replace the code I currently have, and it would probably make the code look a bit cleaner. Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
Simply "converting the code... into code that uses OOP" doesn't mean you're programming in an object-oriented manner. In fact, most likely you are still coding procedurally, and will end up using classes as a pseudo-namespace for functions. The way I like to describe it, is programming OOP is a different mindset than programming OOP, and you really have to think differently in order to truly benefit from those "powerful language constructs". So convert away, but realize there's a lot more to OOP than just "clean code"
Re: Is Procedural looked down upon compared to OOP?
Posted: Tue Mar 10, 2009 1:19 am
by Christopher
swraman wrote:Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
I would not convert existing working code. But as you create new code, or refactor existing code, look for opportunities to use OO design and OOP. Keep at it.