Re: [PHP] advise on simplfying session usage

2012-01-13 Thread David Courtin
In a session context with a lot of session vars, you can use magic methods 
__set and __get :

class s{
private function __set($property, $value){
$_SESSION[$property] = $value ;
}

private function __get($property){
return $_SESSION[$property] ;
}
}

and work with session like an object with the same access :

$s-one = 'one';
$s-oneTwo = array('one', 'two');

echo $s-one;

var_dump($s-oneTwo);

Le 13 janv. 2012 à 04:53, mail.pmpa a écrit :

 When I have many calls to $_SESSION I do:
 
 $s = $_SESSION;
 $s['foo'] = 'bar';
 
 echo $s['foo'];  //bar
 
 -Original Message-
 From: Haluk Karamete [mailto:halukkaram...@gmail.com] 
 Sent: sexta-feira, 13 de Janeiro de 2012 01:17
 To: php-general@lists.php.net
 Subject: [PHP] advise on simplfying session usage
 
 Again, coming from ASP background, I'm trying to minimize the typing for
 most needed functionalities..
 
 in asp, to set a session var, you go %session(age)=90% and to output it,
 you just go %=session(age)%
 
 in php, you've got to _SESSION['age']=90. that's a lot of keyboarding, lots
 of double key strokes and the entire word session has to be uppercase.
 of course, if you use an IDE and you get fast at it, this may not be an
 issue but I wanted to simplify it anyway.
 
 so the plan is this
 
 ?php
 
 _s(age,43) //set the session var age to 43 echo _s(age) //outputs the
 value
 
 ?
 
 To achieve this; I wrote this preliminary function;
 
 function _s($var,$val = r4r53d323,9e809023890j832e@14fdsffdd)
 {
   if ($val == r4r53d323,9e809023890j832e@14fdsffdd)
   {return $_SESSION[$var];}
   else
   {$_SESSION[$var] = $val;}
 }
 
 Now, what's that number you ask!... it's just a value which I figured I
 would never end up in a real app.
 It's just a way for me to use default argument of the function so I can call
 _s function with 1 or 2 arguments.
 
 Can this be done a better way? How do you use _s function with 1 or 2
 arguments so in 1 arg mode, you can use it as a set, and in 2 arg mode, you
 use it as a way to return val.
 
 Is func_get_args route the only way? performance wise which one would
 better?
 
 
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Variable Troubleshooting Code

2012-01-07 Thread David Courtin
Hi,

some pretty natives php functions exists to do the job :
var_export — Outputs or returns a parsable string representation of a variable
debug_zval_dump — Dumps a string representation of an internal zend value to 
output
var_dump — Dumps information about a variable
print_r — Prints human-readable information about a variable

echo 'pre';
print_r( array | object );
echo '/pre';  

Regards.


Le 7 janv. 2012 à 19:00, Donovan Brooke a écrit :

 Hello!,
 I work in another language mostly and often develop while displaying 
 variables (post,get,and defined) and their values at the bottom of the page 
 or in specific places. So, I thought I'd forward my PHP version as an effort 
 of good Karma to the list perhaps! ;-)
 
 Below is 2 simple functions that are helpful for troubleshooting while 
 developing. Just place this code into a .php file and require it at the top 
 of any PHP page. Then, at the bottom of the page, or in a specific (more 
 pertinent) location, call the functions with something like this:
 
 
 ?PHP
 //troubleshooting code
 print 'br /bTesting:/bp';
   
 print htmlentities(list_formvars());
   
 print htmlentities(list_vars(get_defined_vars()));
   
 print '/p';
 ?
 -
 
 Optionally, you can call only specific naming conventions of your variables 
 (if you use them).. ie:
 
 print htmlentities(list_vars(get_defined_vars(),'t_'));
 
 The above will display all defined vars such as:
 
 t_name=value
 t_city=value
 t_address=value
 
 etc..
 
 
 Code:
 ---
 /*
 FUNCTION NAME: list_formvars
   INPUT: optional begins with var
   OUTPUT: Name = Value br /
 Name = Value br /
   USE: For troubleshooting code
 
   Example Use:
  list_formvars();
  list_formvars('f_a');
 
 */function list_formvars($pmatch = null) {
   print br /b'get' Vars:/bbr /;
   foreach ($_GET as $key = $value) {
if (isset($pmatch)) {
   if (substr($key,0,strlen($pmatch)) == $pmatch) {
  print $key = $valuebr /;
   }
} else {
   print $key = $valuebr /;
}
 }
 
   print br /b'post' Vars:/bbr /;
   foreach ($_POST as $key = $value) {
if (isset($pmatch)) {
   if (substr($key,0,strlen($pmatch)) == $pmatch) {
  print $key = $valuebr /;
   }
} else {
   print $key = $valuebr /;
}
 }
 }/*
 FUNCTION NAME: list_vars
   INPUT: get_defined_vars(),begins with match
   OUTPUT: Name = Value br /
 Name = Value br /
   USE: For troubleshooting code
 
   Example Use:
  list_vars(get_defined_vars());
  list_vars(get_defined_vars(),'t_');
 */function list_vars($a_vars,$pmatch = null) {
  print br /b'defined' Vars:/bbr /;
 foreach ($a_vars as $key = $value) {
if (isset($pmatch)) {
   if (substr($key,0,strlen($pmatch)) == $pmatch) {
  print $key = $valuebr /;
   }
} else {
   print $key = $valuebr /;
}
 }
 }
 
 
 Cheers,
 Donovan
 
 
 P.S. Always open to good criticism if you peeps see something that can be 
 written better.. this is about my 3rd PHP project only... so, still heavily 
 learning ;-)
 
 
 -- 
 D Brooke
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php