TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   OOP vs Procedural (http://www.talkphp.com/advanced-php-programming/6096-oop-vs-procedural.html)

maeltar 01-14-2012 03:50 AM

OOP vs Procedural
 
What are the advantages of using oop over procedural ?

Take the following function, and if I were to make this into a class (which I have no idea how to yet!) what would I gain ?

This is to find out the current week number for a company financial year, 4th April 2011 being week 1

PHP Code:

<?php

date_default_timezone_set
('Europe/London');

function 
longDate($uk_date$sep){
    
    list(
$day$month$year) = explode($sep$uk_date); 
$usdate $month.'/'.$day.'/'.$year
return 
date("l, F jS, Y",strtotime($usdate));
    
    
}

function 
getCurrentWeek($type){
$week_secs 604800;
// uk format dd/mm/yyyy
$week1 '04/04/2011';

list(
$day$month$year) = explode('/'$week1);

$startFinYear gmmktime(0,0,0,$month$day$year);

// current unix time
$currTime strtotime('now');

$weekDiff floor(($currTime $startFinYear)/$week_secs);
$timeDiff $currTime $startFinYear;

$shortDate date('d/m/Y'strtotime("+" $weekDiff " week",$startFinYear ));
$longDate longDate(date('d/m/Y'strtotime("+" $weekDiff " week",$startFinYear )), '/');

switch(
$type){
    case 
'short':
        return 
$shortDate;
        break;
    case 
'long' :
        return 
$longDate;
        break;
    default:
        return 
$weekDiff;
        break;
    }


}

echo 
getCurrentWeek('') . "<br />";
echo 
getCurrentWeek('short') . "<br />";
echo 
getCurrentWeek('long') . "<br />";
?>


tony 01-15-2012 05:09 AM

In this case I don't see a gain in making a class out of 1 function. You would gain more if for example you have a class for weeks and there are different actions that a developer can make that class have. For example a developer can make a week to be print out in different formats, to be added or substracted. Stuff like that.

I am not sure if I explained myself well, but there are various reasons to design classes and one of them is to encapsulate common actions (methods) applied to an object.

jkon 02-16-2012 11:22 PM

Don’t see like it is object oriented vs procedural , just one brunch of programming flourished to something of its own. I have seen object oriented thinking in programs written before object oriented came out, in languages that where strict procedurals (like COBOL). The thing with your script is that it has many variables hard coded, and it is ok for now that you have only two functions but what are you going to do next time you want to modify something. You will always include this code and change the variables ?, more over tomorrow you will have more function in the same logic chapter , how will you group them ? Object oriented programming isn’t just a trend, it is the best thing we have so far in logic encapsulation, but as a tool can be used in wrong ways too (I could say more on that but that would be out of topic)

ivanivkovich 05-18-2012 01:51 PM

"What are the advantages of using OOP over procedural?"
-Code reusability.
-OOP has features you can never use with procedural work. Magic methods (example : __get class function (method)is called every time you try to access a variable from object (attribute) that does not exist), so you can do all sorts of tricks with magic methods.
-You split your program in chunks all having their purpose.
-Less code, more functionality. Smart code, in general. In procedural you have to include or copy bunch of stuff over and over again.
-You change a class method, you change it in the whole program.
-I can continue on, but you just have to try it. :P

Take the following function, and if I were to make this into a class (which I have no idea how to yet!) what would I gain ?
-It's not so much in your particular piece of code that would change, your application structure would change, probably resulting in less code.

ivanivkovich 06-01-2012 12:50 PM

Let me put it simpler then:
You can write a whole app with 10 000 lines of tidy code, and the same app with less quality functionality with 90 000 lines of code of mess, too much includes, too much if statements, and too much code that needs to be "shut uff" for most pages.

lennondevid 11-01-2012 07:12 AM

OOP provides a clear modular structure for programs which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface.


php web application development | PHP development | php mysql development | cakephp developers


All times are GMT. The time now is 06:57 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0