Is Procedural looked down upon compared to OOP?
Moderator: General Moderators
Is Procedural looked down upon compared to OOP?
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
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?
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?
I love the warring factions mindset. Balance is the key to true zen.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is Procedural looked down upon compared to OOP?
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.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?
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.
(#10850)
Re: Is Procedural looked down upon compared to OOP?
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?
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.arborint wrote: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.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?
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?
I believe you, but I honestly dont know where OOP can do something that procedural can't...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.
Is there any way to have an input arguement to a class?
Re: Is Procedural looked down upon compared to OOP?
Here is a simple example of a class that represents an object:
And it's usage:
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.
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;
}
}
}
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 />";
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.
- VirtuosiMedia
- Forum Contributor
- Posts: 133
- Joined: Thu Jun 12, 2008 6:16 pm
Re: Is Procedural looked down upon compared to OOP?
I wrote a fairly lengthy post on another forum about OOP vs. Procedural programming. You might find it useful.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Is Procedural looked down upon compared to 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"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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Is Procedural looked down upon compared to 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.swraman wrote:Im just curious as to whether I should actually convert the code I currently have into code that uses OOP.
(#10850)