Thursday, January 22, 2009
Overriding internal function names
Overriding internal function names
While oftentimes, the internal functions in PHP provide great utility, sometimes they don't do exactly what we want them to. We may need to augment their behavior to match what we want the function to do, but we also want to redefine the function with another name to avoid further cluttering the scope.
The filesystem functions are one area where we may want to do this. Let's say we want to make sure any file created by file_put_contents()
has certain permissions set. For example, let's say we want the files created by this to be read-only; we can redefine the function in a new namespace, as shown below.
Listing 11. Defining
file_put_contents()
inside a namespacenamespace Foo;
function file_put_contents( $filename, $data, $flags = 0, $context = null )
{
$return = \file_put_contents( $filename, $data, $flags, $context );
chmod($filename, 0444);
return $return;
}
?>
file_put_contents()
function inside the function and prefix the function name with a backslash to indicate that it should be handled in the global scope, which means the internal function will be called. After calling the internal function, we then chmod()
the file to set the appropriate permissions.These are just a few examples of how we can use namespaces to enhance our code. In each case, we avoid doing ugly hacks, such as prefixing the name of the function or class to make the name unique. We also now know how using namespaces can make it much safer to include third party code in large applications without the worry of name collisions.
For more information, pls visit:
- http://www.ibm.com/developerworks/opensource/library/os-php-5.3new3/index.html?S_TACT=105AGX54&S_CMP=C0122&ca=dnw-1003&open&cm_mmc=4693-_-n-_-vrm_newsletter-_-10731_101903&cmibm_em=dm:0:13624659
- http://in.php.net/manual/en/language.namespaces.faq.php
- http://in.php.net/manual/en/language.namespaces.php
- MySQL LEFT JOIN - http://kathyravan.blogspot.com/2009/01/mysql-left-join.html
- MySQL SQL_CACHE - http://kathyravan.blogspot.com/2008/12/mysql-sqlcache.html
- MySQL Query Caching - http://kathyravan.blogspot.com/2008/12/mysql-query-cache.html
- MySQL Show - http://kathyravan.blogspot.com/2008/12/mysql-show-statements.html
- CSV - MySQL Storage Engine - http://kathyravan.blogspot.com/2008/12/csv-mysql-storage-engine.html
- Blackhole - MySQL Storage Engine - http://kathyravan.blogspot.com/2008/12/blackhole-mysql-storage-engine.html
- Memory - MySQL Storage Engine (Formerly Heap Storage Engine) - http://kathyravan.blogspot.com/2008/12/memory-mysql-storage-engine-formerly.html
- Xml in MySQL - http://kathyravan.blogspot.com/2008/03/xml-in-mysql.html
- Forgot mySQL root Password? - http://kathyravan.blogspot.com/2008/02/forgot-mysql-root-password.html
- Samba - Installation & Configuration - http://kathyravan.blogspot.com/2008/02/samba-installation-configuration.html
- Enabling HTTP Authentication in Apache - http://kathyravan.blogspot.com/2008/08/enabling-http-authentication-in-apache.html
- PHP Function: isFTPFileExists (Searching a FILE in FTP) - http://kathyravan.blogspot.com/2008/12/php-function-isftpfileexists-searching.html
- Shell Scripting & Linux - http://kathyravan.blogspot.com/search/label/Shell%20Scripts
No comments :
Post a Comment