Simple Trait in PHP PRO
Defining a trait is very similar to defining a class. Instead of using the keyword class we use the keyword trait. The purpose of traits is to group functionality in a fine grained and consistent way. It is not possible to instantiate a trait. The trait servers as an additional capability that provides us with additional capabilities when […]
Arrays Short Syntax in PHP 5.4 PRO
As of PHP 5.4 we can create new arrays in a simple way. Simpler than ever. The following code sample shows that. <?php $vec_a = [4,6,2,7]; $vec_b = [‘a’=>’australia’,’b’=>’belgium’,’c’=>’canada’]; foreach($vec_a as $k=>$v) { echo ” “.$k.”=>”.$v; } foreach($vec_b as $k=>$v) { echo ” “.$k.”=>”.$v; } ?> The following video clip shows the execution of this […]
Array Dereference in PHP 5.4 PRO
As of PHP 5.4 we can develop a function that returns an array and treat the call to that function as an array we want to access its elements. <?php function doSomething() { $vec = [23,53,75,64]; return $vec; } echo doSomething()[0]; ?> The following video clip shows the execution of this code sample and explains […]
The PHP 5.2 filter_var Function PRO
PHP 5.2 adds the filter_var function. It is an important function that assists with validating the user input. <?php $email_address_dirty = “haim.michael@gmail.com”; if(filter_var($email_address_dirty,FILTER_VALIDATE_EMAIL)) { $email_address_clean = $email_address_dirty; } echo $email_address_clean; ?> The following video clip shows the execution of this code sample and explains it.
PHP 5.4 Short Tags PRO
As of PHP 5.4, the support for short tags exists by default. There is no need to introduce changes into php.ini in order to use the PHP short tags. <? $numA = 24; $numB = 4; ?> <h1><?=($numA+$numB)?></h1> The following video clip shows the execution of this code sample and explains it.
PHP 5.4 Access Members on Instantiation PRO
PHP 5.4 allows us to access class members on the instantiation. We will use this special capability in those rare cases in which the only reason for creating the object is in order to allow us to access one of the class members. <? class Utils { function calc($numA,$numB) { return $numA+$numB; } } $temp […]
PHP 5.4 Class::{expr}() Syntax PRO
PHP 5.4 provides us with a new way to invoke static functions. This new way allows us storing the function name in a variable (as a string) and place that variable within curly brackets. <? class GoGo { public static function do_something() { echo “something!!!”; } } GoGo::{‘do_something’}(); ?> The following video clip shows the […]
The __get & __set Magical Functions in PHP PRO
Using these two functions we can dynamically add new variables, that weren’t defined in the class, to objects in our program. <? class Bongo { var $vec; function __construct() { $this->vec = array(); } function __get($str) { return $this->vec[$str]; } function __set($var_name,$var_value) { $this->vec[$var_name] = $var_value; } } $ob = new Bongo(); $ob->name=”balaboa”; $ob->id=12123123; echo […]
PHP 5.4 Support for Binary Format PRO
As of PHP 5.4 we can include in our code numbers written in a binary format. We just need to precede these numbers with ‘0b’. <? $a = 0b1110; //14 $b = 0b1011; //11 $c = $a & $b; //0b1010 echo $c; ?> The following video clip shows how this code executes and provides more […]
HIT PHP Web Applications June 2012 INFO
On Monday June 11th I will start teaching the ‘PHP Cross Platforms Web Applications’ course in HIT. It is a 180 academic hours course that focuses on PHP and various other related web technologies. You can find more info together with a contact form at www.xperato.com/hit/hitphp.html.