PHP 5.4 – First Glances

PHP 5.4 is just around the corner, and with it several new features and enhancements. Here are just a few of the features I’ve had the chance to play with that may be useful.

Binary Integer Notation

Previously in PHP, you were able to write integers as decimals (10), Octals (012), and Hexadecimals (0xA). Now in PHP 5.4 you can use binary notation for integers by preceding the binary number with “0b” – such as (0b1010).

PHP

1

2

3

4

5

6

7

8

9

10

11

// Decimal

echo(string)10;// 10

// Octal

echo(string)012;// 10

// Hexadecimal

echo(string)0xA;// 10

// Binary

echo(string)0b1010;// 10

Session Status

PHP 5.4 introduces a new session function designed to explain the current status of the session. The session_status() function will return the following responses:

PHP_SESSION_DISABLED if sessions are disabled.PHP_SESSION_NONE if sessions are enabled, but none exists.PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

For example:

PHP

1

2

3

4

5

<?php

echosession_status();// echos PHP_SESSION_NONE

session_start();

echosession_status();// echos PHP_SESSION_ACTIVE

?>

PHP 5.4 also introduces the SessionHandler class to provide more flexibility and standardization in how sessions are handled.

Arrays

Finally, in PHP you can now use the short bracket syntax for arrays. While really not a huge deal, this aligns PHP with many of the other programming languages, and will probably make switching from another language just a little easier (although array(1,2,3) is pretty simple).

PHP

1

2

3

4

5

$oldArray=array(1,2,3);

echo(string)$oldArray[1];// 2

$newArray=[1,2,3];

echo(string)$newArray[1];// 2

Another nice feature in regards to arrays is Array Dereferencing, or being able to access an array object directly off of a method or function.

PHP

1

2

3

4

5

6

7

8

9

10

11

functiongetArray()

{

returnarray(1,2,3);

// or return [1,2,3]; in PHP 5.4 ;)

}

// PHP 5.3

echo(string)getArray()[1];// error

// PHP 5.4

echo(string)getArray()[1];// 2

Class Instantiation Chaining

Also in PHP 5.4, class instantiation chaining was added, allowing you to chain off of the instantiation of a class by using parentheses.

PHP

1

2

3

4

5

6

7

8

9

10

11

classTest

{

publicfunctionhello()

{

echo'hello world';

}

}

// in PHP 5.3 this would be an error

// in PHP 5.4 it echos 'hello world'

echo(newTest())->hello();

Traits

Ok, this is a super simple overview of traits, but the addition of traits provides the flexibility of “extending” multiple classes without having to go through an entire chain. Unlike many languages, PHP Traits can also contain properties, however, if the class contains a property with the same name it will throw an E_STRICT warning IF the class property === the trait property, or a fatal error if the class property !== the trait property.

To use a trait, you would write something like this:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

trait MyTrait()

{

public$MyProperty='hello';

publicfunctionMyMethod()

{

echo'world';

}

}

classMyClass()

{

useMyTrait;

publicfunctionDoStuff()

{

echo$this->MyProperty;

echo' ';

$this->MyMethod();

}

}

(newMyClass())->DoStuff();// echos out 'hello world'

More to Come!

There’s a lot more features in PHP 5.4 that I still need to explore, including PHP Session Uploading, improved Closures, and a built in HTTP server. So stay tuned and I will try to give a quick overview of those as well as soon as I can get enough time to play with them