php

Read Folder

This is a Lambda function also called anonymous function within a regular function that scans a given directory and it’s sub directories returning an array of all php files within. Following is a snip output of the Zend files. [6] => /usr/share/php/Zend/Captcha/ReCaptcha.php [7] => /usr/share/php/Zend/Captcha/Base.php [8] => /usr/share/php/Zend/Feed/Rss.php [9] => /usr/share/php/Zend/Feed/Atom.php [52] => /usr/share/php/Zend/Feed/Writer/Source.php [53] => /usr/share/php/Zend/Feed/Pubsubhubbub.php [54] => /usr/share/php/Zend/Feed/Element.php [55] => /usr/share/php/Zend/Feed/Reader/Feed/Rss.php [90] => /usr/share/php/Zend/Feed/Builder.php [91] => /usr/share/php/Zend/Feed/Writer.php [92] => /usr/share/php/Zend/Config.

Autoload

I have been looking around for some good examples to use the new PHP autoloader interoperability standard according to the PHP Standards Recommendation #0 (PSR-0). And since I could find anything good, I had to figure it out by my self, here is the result. gist id=3027247 HowTo: <?php # Adds your library to the beginning of the php include path addInclude('~/code/libs', FALSE); $loader = new SplClassLoader(); # adds the php include path to the class $loader->setIncludePathLookup(true); $loader->add('Zend'); $loader->add('PEAR'); # Give the path if it isn't in the php include path like this $loader->add('Symfony', '/path/to/symfony'); $loader -> register(); ?

Messenger Laravel Framework

This is a fork and upgrade of the davzie message class. A info, error etc messenger class that allows you to add validation errors, statuses etc to flash data and then retrieve them in a nicely formatted way in your front-end. Automates the process a little when you’re adding and retrieving messages, especially in a CRUD type environment. The formatting integrates well with Twitter’s Bootstrap Alert formatting. This upgraded version works smoothly with the Laravel validation class

Multiple PHP installations

How to install PHP 5.3.X as secondary php version from source PHPV=5.3.6 cd /usr/local/src wget http://us.php.net/get/php-$PHPV.tar.gz/from/this/mirror mv mirror php-$PHPV.tar.gz tar xzvf php-$PHPV.tar.gz chown -R $USER:$USER php-$PHPV/ cd php-$PHPV/ apt-get install libxml2-dev libssl-dev libcurl4-gnutls-dev libjpeg62-dev libpng12-dev libfreetype6-dev unixodbc-dev ./configure --prefix=/usr/local/php5.3 --with-config-file-path=/usr/local/etc/php5.3 --with-config-file-scan-dir=/usr/local/etc/php5.3/$SAPIconf.d --with-libdir=/lib --disable-cgi --with-libxml-dir=/usr/ --with-mysql=/usr/ --enable-pdo --with-pdo-mysql --with-mysqli --with-zlib-dir=/usr --with-curl --with-gd --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib --with-freetype-dir=/usr/lib --with-gettext --enable-mbstring --enable-soap --enable-ftp --enable-fpm --with-openssl make make test make install More help in case you have errors: PHP-Configure-und-Compile-Fehler

fixing php's gettype function

<?php public static function getType($value, $max_length = 50){ $type = gettype($value); if($type == 'NULL' || $type == 'boolean' || $type == 'integer' || $type == 'double' || $type == 'object' || $type == 'resource' || $type == 'array' ) return array('type'=>$type,'value'=>$value); if($type == 'string' && empty($value)) return array('type'=>'NULL','value'=>$value); if($type == 'string' && strlen($value) > $max_length) return array('type'=>'blob','value'=>$value); if($type == 'string' && substr($value, 0,1) === '0') return array('type'=>'string','value'=>$value); if($type == 'string' && is_numeric($value)){ $int = (int) $value; $float = (float) $value; if($int == $value){ $value = $int; $type = 'integer'; }elseif($float == $value){ $value = $float; $type = 'double'; } }elseif($type == 'string'){ $type = 'string'; }else{ $type = 'blob'; } return array('type'=>$type,'value'=>$value); } ?

num_row with PDO

There is no num_row() function for PHP PDO Here are my two solutions: One with prepare & execute: db->prepare($sql); $sth->execute(array($key)); $rows = $sth->fetch(PDO::FETCH_NUM); echo $rows[0]; One with query: db->query($sql); $row = $result->fetch(PDO::FETCH_NUM); echo $row[0];

e() for echo

It’s really annoying having to thing of html stuff like pre & br everytime you need to display something. And it’s even worst if you like me test your code in cli and cgi. So I wrote this little method to take care of it all. It’s nothing complicated but sure useful. It detects cgi and cli, and formats everything accordingly. <?php /** * e() --- Prints human-readable information about a variable * * string e ( mixed $expression[, string $name [, bool $return = false ]] ) * * Replacement for php echo, print, print_r(), var_export() etc */ /** * @category TaMeR * @copyright Copyright (c) 2008 - 2011 Dennis T Kaplan * @license http://www.

Creating models for the Zend Framework from command line via phpcli

This is a php cli script that will create a database model from the database provided Download via git from github -> phpcli php zend/model.php -h This is a command line PHP script. Set table Options: -path=[ ] -table=[ ] Usage: php zend/model.php -arg=value Database Structure: CREATE TABLE users ( Alias TEXT UNIQUE, Password TEXT, eMail TEXT, Avatar TEXT, Timezone TEXT, IP TEXT, count INTEGER, cookie TEXT, vars TEXT, status INTEGER DEFAULT 1, added date, updated datetime, PRIMARY KEY(Alias) ); Sample Output:

PHP goto statement

I just discovered that the goto statement is available since php 5.3 I love the guys from php for that. I am a hobby programmer and unlike the professionals, I learned OOP via php, which is great but having goto available like back in the Amiga days made my day.

Zend ACL

Tamer_ACL is a simple Zend Framework ACL plugin that uses the sqlite backend to store all the data. How To Inatall Make sure you have a table called role in your authentication table and a table called alias you may change that to username if you like. The sql code is below. Below are the files. Before each file you will find installation instructions that should work even for the most novice Linux / Zend Framework developer.