Function for looking if it is a NULL
<?php
$var = NULL;
if (isnull("var")) {
echo "var===NULL\n";
} else {
echo "var!==NULL\n";
}
if (isnull("test")) { // give FALSE, test is not set
echo "test===NULL\n";
} else {
echo "test!==NULL\n";
}
$array['var'] = NULL;
if (isnull("var", $array)) {
echo "array['var']===NULL\n";
} else {
echo "array['var']!==NULL\n";
}
function isnull($var, $base = FALSE) {
if ($base===FALSE) {
$base = &$GLOBALS;
} elseif (!is_array($base)) {
return FALSE;
}
if ((array_key_exists($var, $base))&&($base[$var]===NULL)) {
return TRUE;
} else {
return FALSE;
}
}
?>
Saturday, October 20, 2007
NULL
POSTED BY
Oriol
AT
4:01 PM
Callback function
<?php
// An example callback function
function my_callback_function() {
echo 'hello world!';
}
// An example callback method
class MyClass {
function myCallbackMethod() {
echo 'Hello World!';
}
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));
// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
?>
Other example
This's a useful example about callback, Look at the session_set_save_handler function.
<?php
/* Create new object of class */
$ses_class = new session();
/* Change the save_handler to use the class functions */
session_set_save_handler (array(&$ses_class, '_open'),
array(&$ses_class, '_close'),
array(&$ses_class, '_read'),
array(&$ses_class, '_write'),
array(&$ses_class, '_destroy'),
array(&$ses_class, '_gc'));
/* Start the session */
session_start();
class session
{
/* Define the mysql table you wish to use with
this class, this table MUST exist. */
var $ses_table = "sessions";
/* Change to 'Y' if you want to connect to a db in
the _open function */
var $db_con = "Y";
/* Configure the info to connect to MySQL, only required
if $db_con is set to 'Y' */
var $db_host = "localhost";
var $db_user = "username";
var $db_pass = "password";
var $db_dbase = "dbname";
/* Create a connection to a database */
function db_connect() {
............
}
/* Open session, if you have your own db connection
code, put it in here! */
function _open($path, $name) {
.............
}
/* Close session */
function _close() {
..............
}
/* Read session data from database */
function _read($ses_id) {
.................
}
/* Write new data to database */
function _write($ses_id, $data) {
...........
}
/* Garbage collection, deletes old sessions */
function _gc($life) {
............
}
}
?>
POSTED BY
Oriol
AT
3:57 PM
Thursday, October 18, 2007
Create Items In an Array
If you use new to create items in an array, you may not get the results you want since the parameters to array will be copies of the original and not references.
By Example:
class Store {
var $item = 3;
}
$a = array( new Store() );
$b = $a;
$a[0]->item = 2;
print( "|" . $b[0]->item . "|
" ); //shows 3
$a = array();
$a[] =& new Store();
$b = $a;
$a[0]->item = 2;
print( "|" . $b[0]->item . "|
" ); //shows 2
This is extremely important if you intend on passing arrays of classes to functions and expect them to always use the same object instance!
Note: The following syntax is desired (or maybe even the default notation should translate as this):
$a = array( &new Store() );
POSTED BY
Oriol
AT
1:15 PM
Monday, October 8, 2007
Complex (curly) syntax
<?php
// Let's show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong
// outside a string. In other words, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "You can even write {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
?>
POSTED BY
Oriol
AT
4:04 PM