<?php
//One could use array_walk, altough i think it was fun
//and simple doing it this way.
class oop {
//toObject : Transforms an array into an object filtering it
//$source : Array to transform
//$currentLevel : See $maxLevels
//$maxLevels : Protect the system in case of lots of recursion
// i.e. <input type="text" name="test[][]....[N]"
public static function toObject($source=array(),$array=array(),$maxLevels=3,$currentLevel=0) {
if ( !sizeof($source) || ($currentLevel > $maxLevels) ) return FALSE;
$array = (sizeof($array)) ? $array : $source;
$obj = new stdClass();
foreach ($array as $k => $v){
if (is_array($v)) {
$obj->$k = self::toObject($source,$v,$maxLevels,++$currentLevel);
continue;
}
//Assign to the object $obj, the key and the value of the actual value of $source
$obj->$k=$v;
}
return $obj;
}
}
/* Eexamples
$post = oop::toObject($_POST) ;
$get = oop::toObject($_GET) ;
$session = oop::toObject($_SESSION);
var_dump ($post) ;
var_dump ($get) ;
var_dump ($session);
*/
?>
Por que não usar Magic Quotes?
- Portabilidade Presumir que ela está ligada, ou desligada, afeta portabilitade. Use Assuming it to be on, or off, affects portability. Use get_magic_quotes_gpc() para verificar isso e codifique de acordo com a situação.
- Performance Como nem todos os dados escapados são inseridos em um banco de dados, existe uma perda de performance por escapar todos os dados. Chamar funções de escape (como addslashes()) em tempo de execução é mais eficiente. Embora o arquivo php.ini-dist habilita essas diretivas por padrão, php.ini-recommended desabilita ela. Essa recomendação é principalmente por razões de performance.
- Inconviniência Porque nem todos os dados precisam ter caracteres de escape inseridos, é irritante ver caracteres de escape onde não deviam. Por exemplo, mandar um e-mail por um formulário, e ver um monte de \' na mensagem. Para consertar isso, pode ser necessário o uso excessivo da função stripslashes().
Por que não usar Magic Quotes?
estoesunapija at hotmail dot com
15-Sep-2008 11:26
15-Sep-2008 11:26
sir dot steve dot h+php at gmail dot com
06-Dec-2007 09:45
06-Dec-2007 09:45
I find it useful to define a simple utility function for magic quotes so the application functions as expected regardless of whether magic_quotes_gpc is on:
function strip_magic_slashes($str)
{
return get_magic_quotes_gpc() ? stripslashes($str) : $str;
}
Which can be annoying to add the first time you reference every $_GET /$_POST/$_COOKIE variable, but it prevents you from demanding your users to change their configurations.
rjh at netcraft dot com
13-Jun-2007 03:50
13-Jun-2007 03:50
Additionally, addslashes() is not a cure-all against SQL injection attacks. You should use your database's dedicated escape function (such as mysql_escape_string) or better yet, use parameterised queries through mysqli->prepare().
gerard at modusoperandi dot com dot au
13-May-2007 10:53
13-May-2007 10:53
Apparently it will be removed in PHP 6:
http://www.php.net/~derick/meeting-notes.html#magic-quotes
11-Feb-2006 02:47
It is also important to disable Magic Quotes while in development enivronment. For the reasons mentioned above, not everybody is using Magic Quotes.
An application that works fine with Magic Quotes enabled may have security problems (ie can be subject to SQL attacks) when distributed.
