RSS
 

Posts Tagged ‘PHP5.4’

Update your Server to PHP5.4+

13 Apr

With many new features and more speed it makes sense to upgrade to PHP5.4.
It is also a requirement when you start using CakePHP3.0 (the dev releases) or code that contains short array syntax.
So the sooner you upgrade the better.

The following examples are for Debian Squeeze.

Backup

Make sure you have a backup of your server/files/db and your php.ini and other config files.

Updating

Debian doesn’t offer this natively.
We need to add some special packages for this.

Open the sources list using nano, vi or the edit tool of your choice:

vi /etc/apt/sources.list

And add those two lines at the end:

deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all
deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all

We need a public key to avoid those warnings showing up:

wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -

Use apt-get update and apt-get install php5.

That should take care of it. PHP5.4.x should now be installed and ready to work with.
And have fun playing around with CakePHP3.0 or some new features of PHP5.4 🙂

Keep other packages up to date

Also make sure other packages are up to date using apt-get upgrade.

If it doesn’t install right away, one might also need apt-get dist-upgrade. Careful with this, though. It might install/overwrite more than it should.

Dev Libraries

If php5-dev libraries are needed, they can be installed via apt-get install php5-dev.

Key Benefits of 5.4

  • Speed/Stability
  • Traits
  • Closures and $this
  • callable as typehint
  • Short array syntax (no mixin of method brackets and array brackets anymore)

The only real benefit coming in 5.5 is "Generators" which will allow handling of huge arrays and larger amount of data more gracefully.
But other than that 5.4 is just fine for the moment.

Features of 5.3 you can now also use in Cake3

  • Namespaces
  • Closures
  • Late Static Bindings
  • Ternary shorthand form
  • Magic method __callStatic()
  • Nested exceptions

These features weren’t used in Cake2 core (and probably most apps) so far due to the BC for PHP5.2. If you already updated your minimum requirement for your server and application, those
are also now of great use.

Further tips

The encoding even in more current verions of apache usually defaults to the old "ISO-8859-1" one. You should make it "utf-8" per default in your php.ini:

; PHP's default character set is set to empty.
; http://php.net/default-charset
default_charset = "utf-8"

Then even error pages or non-normal responses will be properly displayed. It is sad that even 2014 and in PHP5.4 the default is still not a proper one – probably due to BC or something.

 
No Comments

Posted in PHP

 

PHP5.4 is out

03 Mar

In case you haven’t heard yet (pretty much impossible, isn’t it): PHP5.4 has been released.

In a previous post I already pointed out the possibilities for traits – although I am not quite sure yet how it could benefit current cake projects.

This article describes some more news and features regarding this new release.

I certainly won’t use any of the "syntactic sugar" like short array syntax anytime soon. It would just break with the current way of writing arrays – and then one would end up with two different syntaxes in the same app or even class.
But some of new features (besides the traits) sure look promising!

What will you be using? Or is there – especially regarding cake – no real use for any of those new features?

 
No Comments

Posted in PHP

 

Traits – PHP5.4

31 Dec

Since it is new years eve, I don’t want to start a complete new chapter.
So I will write one last post this year with some more general topic.
And yes – I find it so interesting that I have to mention it in an own post 🙂

Traits

With PHP5.4 approaching fast, it will probably be the standard sometime in 2012.
It will have one major feature that we will all going to love: oop5.traits.

What are they?
The above link describes it pretty good. They are some kind of behavior – like the one we attach to models right now. Only way more flexible and powerful.
Every class can then use those behaviors.

trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}
trait World {
    public function sayWorld() {
        echo 'World!';
    }
}
trait HelloWorld {
    use Hello, World;
}
class MyHelloWorld {
    use HelloWorld;
}
$Foo = new MyHelloWorld();
$Foo->sayHello();
$Foo->sayWorld();

The result would be Hello World!

Imagine what would be possible with Controllers, Components, Shells, and other classes that can now not only extend parent classes but also use traits.

Cool things you can do with them

The above link contains a good set of examples.
I will outline the most interesting ones:

public function sayWhere() {
    echo __CLASS__; // same with __FILE__ etc
}

As of right now objects that are extended by some subclass would always return the wrong class, because inside a class those magic constants always relate to the current file (and therefore often the parent class defining it).
With traits the magic is endless – they will make it possible to inject functionality that overcomes that deficiency:

trait sayWhere {
    public function whereAmI() {
        echo __CLASS__;
    }
}
class Hello {
    use sayWHere;
}
class World {
    use sayWHere;
}
$One = new Hello;
$One->whereAmI(); //Hello
$Two = new World;
$Two->whereAmI(); //World

Another neat feature is, that we can override specific methods if needed – both ways:

class Base {
	use SayBye;
	// we want this method to be overridden by the trait to do some extra stuff with it
	// can only be done in the inheriting class, though
	public function sayHello() {
		echo 'Hello ';
	}
}
trait SayWorld {
	public function sayHello() {
		parent::sayHello();
		echo 'World!';
	}
}
trait SayBye {
	// this method will be be overridden by the class method to do some extra stuff with it
	// can only be done in the inheriting class, though
	public function sayBye() {
		echo 'Bye ';
	}
}
class MyHelloWorld extends Base {
	use SayWorld;
	public function sayBye() {
		echo parent::sayBye();
		echo 'World!';
	}
}
$Foo = new MyHelloWorld();
$Foo->sayHello(); //Hello World!
$Foo->sayBye(); //Bye World!

Update 2012-02-24: Interesting Link

There is an interesting page about the potential dangers of those new PHP5.4 features you might want to read before starting to write cutting-edge code 🙂

 
No Comments

Posted in PHP