PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 2nd, 2013, 02:47 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Static versus reference versus global

Hi guys I was fiddling a bit with an error reporting array (storing). Ideally I want to have several functions that pass along what goes wrong to an accessible array (so outside the scope of the function). For instance "creditcard number is invalid" (or whatever).

The code below shows 4 (simplistic) methods to accomplish this. Apart from the first one they all have something I like.

Anyone might want to say which method they favour or dislike and why?
PHP Code:
<?php
/* #1 | most inefficient way */
$errors = array();

function 
bla1($a){
    
$a[] = 'some error text';
    return 
$a;
}

$errors bla1($errors);

var_dump($errors);

/* #2 | with a pass by reference */

$errors = array();

function 
MyReference(&$a){
    
$a[] = 'some error text';

}

MyReference($errors);

var_dump($errors);

/* #3 | with a global variable */

$errors = array();
function 
MyGlobal(){
    global 
$errors;
    
$errors[] = 'terror text';
}

MyGlobal();

var_dump($errors);



/* #4 | with a static function */
class Errors{
    private static 
$errors = array();
    
    public static function 
setErrors($a){
        
self::$errors[]= $a;
    }
    public static function 
getErrors(){
        return 
self::$errors;
    }
}


function 
MyStatic(){
    
Errors::setErrors('error text');
}

MyStatic();

var_dump(Errors::getErrors());

?>


P.s. fairly new to oop so if something is odd, please tell me
__________________
PHP Tutorial

Last edited by aeternus : January 2nd, 2013 at 03:08 PM.

Reply With Quote
  #2  
Old January 2nd, 2013, 03:36 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,697 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 50 m
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Well, how far OOP do you want to go? I would have an object representing the form itself (doesn't have to be so specific as to be for that individual form) which acts like a data repository. You can get the form data from it, even as simply as wrapping functionality around $_POST, and push validation/error messages into it.

In general if you're considering anything remotely OOP then global variables and static/global functions are the wrong direction.

Reply With Quote
  #3  
Old January 2nd, 2013, 03:44 PM
aeternus's Avatar
aeternus aeternus is offline
For POny!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: Amsterdam
Posts: 416 aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level)aeternus User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 5 Days 4 h 56 m 43 sec
Reputation Power: 114
Quote:
Originally Posted by requinix
Well, how far OOP do you want to go? I would have an object representing the form itself (doesn't have to be so specific as to be for that individual form) which acts like a data repository. You can get the form data from it, even as simply as wrapping functionality around $_POST, and push validation/error messages into it.

In general if you're considering anything remotely OOP then global variables and static/global functions are the wrong direction.


Ha Requinix, As always thanks for your reply! I guess your right. This isn't really object orientated and I am more or less abusing it with static functions. I should indeed create some form class. (lazy as always )
The pass by reference I like, but it's a bit of a pain in the * to add all those parameters. Hopefully I can soon show off my Form class. (edit: trying to build it now

Thanks!

Last edited by aeternus : January 2nd, 2013 at 03:50 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Static versus reference versus global

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap