TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-14-2012, 03:50 AM   #1 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default 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 />";
?>
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 01-15-2012, 05:09 AM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 02-16-2012, 11:22 PM   #3 (permalink)
The Visitor
 
jkon's Avatar
 
Join Date: Feb 2012
Posts: 2
Thanks: 0
jkon is on a distinguished road
Default

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)
jkon is offline  
Reply With Quote
Old 05-18-2012, 01:51 PM   #4 (permalink)
Ivan Ivković, Croatia
 
ivanivkovich's Avatar
 
Join Date: May 2012
Posts: 7
Thanks: 0
ivanivkovich is on a distinguished road
Default

"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.
Send a message via Skype™ to ivanivkovich
ivanivkovich is offline  
Reply With Quote
Old 06-01-2012, 12:50 PM   #5 (permalink)
Ivan Ivković, Croatia
 
ivanivkovich's Avatar
 
Join Date: May 2012
Posts: 7
Thanks: 0
ivanivkovich is on a distinguished road
Default

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.
Send a message via Skype™ to ivanivkovich
ivanivkovich is offline  
Reply With Quote
Old 11-01-2012, 07:12 AM   #6 (permalink)
The Wanderer
 
Join Date: Oct 2012
Posts: 10
Thanks: 0
lennondevid is on a distinguished road
Default

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
lennondevid is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
What design pattern? Haris Absolute Beginners 9 10-02-2007 01:14 AM


All times are GMT. The time now is 04:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design