Closed Thread
Differences between PHP4 and PHP5
Old 02-28-2007, 10:36 AM Differences between PHP4 and PHP5
Christopher's Avatar
Iced Cap

Posts: 3,113
Location: Toronto, Ontario
Trades: 0
Here's a quick overview of what has changed between PH4 and PHP5. PHP5 for the most part is backwards compatible with PHP4, but there are a couple key changes that might break your PHP4 script in a PHP5 environment. If you aren't already, I stronly suggest you start developing for PHP5. Many hosts these days offer a PHP5 environment, or a dual PHP4/PHP5 setup so you should be fine on that end. Using all of these new features is worth even a moderate amount of trouble you might go through finding a new host!

Note: Some of the features listed below are only in PHP5.2 and above.

Object Model
The new OOP features in PHP5 is probably the one thing that everyone knows for sure about. Out of all the new features, these are the ones that are talked about most!

Passed by Reference
This is an important change. In PHP4, everything was passed by value, including objects. This has changed in PHP5 -- all objects are now passed by reference.

PHP Code:
$joe = new Person();
$joe->sex 'male';

$betty $joe;
$betty->sex 'female';

echo 
$joe->sex// Will be 'female' 
The above code fragment was common in PHP4. If you needed to duplicate an object, you simply copied it by assigning it to another variable. But in PHP5 you must use the new clone keyword.

Note that this also means you can stop using the reference operator (&). It was common practice to pass your objects around using the & operator to get around the annoying pass-by-value functionality in PHP4.

Class Constants and Static Methods/Properties
You can now create class constants that act much the same was as define()'ed constants, but are contained within a class definition and accessed with the :: operator.

Static methods and properties are also available. When you declare a class member as static, then it makes that member accessible (through the :: operator) without an instance. (Note this means within methods, the $this variable is not available)

Visibility
Class methods and properties now have visibility. PHP has 3 levels of visibility:
  1. Public is the most visible, making methods accessible to everyone and properties readable and writable by everyone.
  2. Protected makes members accessible to the class itself and any subclasses as well as any parent classes.
  3. Private makes members only available to the class itself.
Unified Constructors and Destructors
PHP5 introduces a new unified constructor/destructor names. In PHP4, a constructor was simply a method that had the same name as the class itself. This caused some headaches since if you changed the name of the class, you would have to go through and change every occurrence of that name.

In PHP5, all constructors are named __construct(). That is, the word construct prefixed by two underscores. Other then this name change, a constructor works the same way.

Also, the newly added __destruct() (destruct prefixed by two underscores) allows you to write code that will be executed when the object is destroyed.

Abstract Classes
PHP5 lets you declare a class as abstract. An abstract class cannot itself be instantiated, it is purely used to define a model where other classes extend. You must declare a class abstract if it contains any abstract methods. Any methods marked as abstract must be defined within any classes that extend the class. Note that you can also include full method definitions within an abstract class along with any abstract methods.

Interfaces
PHP5 introduces interfaces to help you design common APIs. An interface defines the methods a class must implement. Note that all the methods defined in an interface must be public. An interface is not designed as a blueprint for classes, but just a way to standardize a common API.

The one big advantage to using interfaces is that a class can implement any number of them. You can still only extend on parent class, but you can implement an unlimited number of interfaces.

Magic Methods
There are a number of "magic methods" that add an assortment to functionality to your classes. Note that PHP reserves the naming of methods prefixed with a double-underscore. Never name any of your methods with this naming scheme!

Some magic methods to take note of are __call, __get, __set and __toString. These are the ones I find most useful.

Finality
You can now use the final keyword to indicate that a method cannot be overridden by a child. You can also declare an entire class as final which prevents it from having any children at all.

The __autoload Function
Using a specially named function, __autoload (there's that double-underscore again!), you can automatically load object files when PHP encounters a class that hasn't been defined yet. Instead of large chunks of include's at the top of your scripts, you can define a simple autoload function to include them automatically.

PHP Code:
function __autoload($class_name) {
     require_once 
"./includes/classes/$class_name.inc.php";

Note you can change the autoload function or even add multiple autoload functions using spl_autoload_register and related functions.

Standard PHP Library
PHP now includes a bunch of functionality to solve common problems in the so-named SPL. There's a lot of cool stuff in there, check it out!

For example, we can finally create classes that can be accessed like arrays by implementing the ArrayAccess interface. If we implement the Iterator interface, we can even let our classes work in situations like the foreach construct.


Miscellaneous Features

Type Hinting
PHP5 introduces limited type hinting. This means you can enforce what kind of variables are passed to functions or class methods. The drawback is that (at this time), it will only work for classes or arrays -- so no other scalar types like integers or strings.

To add a type hint to a parameter, you specify the name of the class before the $. Beware that when you specify a class name, the type will be satisfied with all of its subclasses as well.

PHP Code:
function echo_user(User $user) {
    echo 
$user->getUsername();

If the passed parameter is not User (or a subclass of User), then PHP will throw a fatal error.

Exceptions
PHP finally introduces exceptions! An exception is basically an error. By using an exception however, you gain more control the simple trigger_error notices we were stuck with before.

An exception is just an object. When an error occurs, you throw an exception. When an exception is thrown, the rest of the PHP code following will not be executed. When you are about to perform something "risky", surround your code with a try block. If an exception is thrown, then your following catch block is there to intercept the error and handle it accordingly. If there is no catch block, a fatal error occurs.

PHP Code:
try {
    
$cache->write();
} catch (
AccessDeniedException $e) {
    die(
'Could not write the cache, access denied.');
} catch (
Exception $e) {
   die(
'An unknown error occurred: ' $e->getMessage());

E_STRICT Error Level
There is a new error level defined as E_STRICT (value 2048). It is not included in E_ALL, if you wish to use this new level you must specify it explicitly. E_STRICT will notify you when you use depreciated code. I suggest you enable this level so you can always stay on top of things.

Foreach Construct and By-Reference Value
The foreach construct now lets you define the 'value' as a reference instead of a copy. Though I would suggest against using this feature, as it can cause some problems if you aren't careful:

PHP Code:
foreach($array as $k => &$v) {
    
// Nice and easy, no working with $array[$k] anymore
    
$v htmlentities($v);
}

// But be careful, this will have an unexpected result because
// $v will still be a reference to the last element of the $array array
foreach($another_array as $k => $v) {




New Functions

PHP5 introduces a slew of new functions. You can get a list of them from the PHP Manual.

New Extensions
PHP5 also introduces new default extensions.
  • SimpleXML for easy processing of XML data
  • DOM and XSL extensions are available for a much improved XML-consuming experience. A breath of fresh air after using DOMXML for PHP4!
  • PDO for working with databases. An excellent OO interface for interacting with your database.
  • Hash gives you access to a ton of hash functions if you need more then the usual md5 or sha1.
Compatibility Issues
The PHP manual has a list of changes that will affect backwards compatibility. You should definately read through that page, but here is are three issues I have found particularly tiresome:
  • array_merge() will now give you warnings if any of the parameters are not arrays. In PHP4, you could get away with merging non-arrays with arrays (and the items would just be added if they were say, a string). Of course it was bad practice to do this to being with, but it can cause headaches if you don't know about it.
  • As discussed above, objects are now passed by references. If you want to copy a object, make sure to use the clone keyword.
  • get_*() now return names as they were defined. If a class was called MyTestClass, then get_class() will return that -- case sensitive! In PHP4, they were always returned in lowercase.
Christopher is offline
View Public Profile Visit Christopher's homepage!
 
 
When You Register, These Ads Go Away!
Old 02-28-2007, 12:45 PM Re: Differences between PHP4 and PHP5
tripy's Avatar
Do not try this at home!

Posts: 3,139
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Wow...
Impressive...

Thank you for the time you must have taken to put this together, Schroeder. I learnt a few interesting things here.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is online now
View Public Profile Visit tripy's homepage!
 
Old 02-28-2007, 07:58 PM Re: Differences between PHP4 and PHP5
metho's Avatar
Ultra Talker

Posts: 346
Trades: 0
gg! - sticky?
metho is offline
View Public Profile
 
Old 02-28-2007, 08:09 PM Re: Differences between PHP4 and PHP5
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,674
Name: John Alexander
Trades: 0
Wow! They made PHP an awful lot more like ASP.NET. Good stuff!
Learning Newbie is offline
View Public Profile
 
Old 03-01-2007, 03:57 AM Re: Differences between PHP4 and PHP5
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,167
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
Quote:
Originally Posted by metho View Post
gg! - sticky?
metho, you can use this post: PHP Readme: Tips, Tricks and Must Read Threads to get here at any time
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
View Public Profile
 
Old 03-11-2007, 02:15 PM Re: Differences between PHP4 and PHP5
Junior Talker

Posts: 1
Name: archippus
Trades: 0
please my name is
archippus
can anyone tell how to register mailer php
and how to us it?
thanks bye...
am expecting your reply.
archippus is offline
View Public Profile
 
Old 03-30-2007, 12:25 PM Re: Differences between PHP4 and PHP5
charlesgan's Avatar
hosting-rebate.com

Latest Blog Post:
Hostgator Coupons $93
Posts: 280
Location: hosting-rebate.com
Trades: 0
nice list. i will need to view this few times to get them digested
indead its become more common that customer is requesting php 5 and some are asking php 5.2 already. its fast.
charlesgan is offline
View Public Profile Visit charlesgan's homepage!
 
Old 05-19-2007, 07:40 AM php
Junior Talker

Posts: 2
Trades: 0
in php 3 version the error occur " exception handling" and u u may have add two headers in one page
anwar_auctions is offline
View Public Profile
 
Old 07-05-2007, 01:24 PM Re: Differences between PHP4 and PHP5
dougadam's Avatar
The Watkins Man

Posts: 350
Name: Douglas Adams
Location: Traverse City, Michigan
Trades: 0
Thanks for the very useful and informative thread.
dougadam is offline
View Public Profile Visit dougadam's homepage!
 
Old 07-09-2007, 08:00 AM Re: Differences between PHP4 and PHP5
ved123's Avatar
Super Talker

Posts: 104
Name: Ved
Trades: 0
good one
ved123 is offline
View Public Profile
 
Old 07-14-2007, 09:53 AM Re: Differences between PHP4 and PHP5
Mattmaul1992's Avatar
Ultra Talker

Posts: 485
Name: Matt
Trades: -1
Everyone really needs to learn this because PHP 4 will no longer be supported by the end of this year (2007). Plus with PHP 6 around the corner (well.. Sort of.). Those innovative PHP 5 users will continue their ways with PHP 6. And those slightly behind people will most likely end up with PHP 5. Keep up with the times . If this has already been mentioned then sorry.
__________________
PHP Code:
$talkupation++; 
http://www.forum-front.com/ - Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
View Public Profile
 
Old 07-16-2007, 09:11 AM Re: Differences between PHP4 and PHP5
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,513
Name: Dan
Location: Swindon
Trades: 0
gd post, very helpful... of course most of the stuff u said changed in PHP i dont know anyway... 0.o... YET!

Dan.
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<
dansgalaxy is offline
View Public Profile Visit dansgalaxy's homepage!
 
Old 07-19-2007, 12:48 PM Re: Differences between PHP4 and PHP5
Novice Talker

Posts: 12
Trades: 0
thanks for the thread... would be in trouble without that!
mirza is offline
View Public Profile
 
Old 07-24-2007, 07:37 PM Re: Differences between PHP4 and PHP5
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,513
Name: Dan
Location: Swindon
Trades: 0
I feel a disturbence in the force, must have been that spam i just read.

*cough* ^ SPAM ^ ABOVE *cough*
Dan

EDIT: ****! i ment to give bad TP to linux900 but accidently gave good so now he has 21 when he had -2 eff it. ¬.¬ can a admin take it back for me?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Last edited by dansgalaxy; 07-24-2007 at 07:40 PM..
dansgalaxy is offline
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-03-2007, 06:04 AM Re: Differences between PHP4 and PHP5
twin's Avatar
Average Talker

Posts: 27
Location: TurnkeyForms.com
Trades: 0
Great thread, I haven't seen that list before, very useful
__________________
Private Label Articles | Website Templates | Header Templates
Content Databases - Add quality content to your website
twin is offline
View Public Profile Visit twin's homepage!
 
Old 08-08-2007, 11:34 AM Re: Differences between PHP4 and PHP5
lajocar's Avatar
Ultra Talker

Posts: 465
Name: Lawrence
Location: South Africa
Trades: 0
thanks for the thread

It is interesting to see the differences between PHP 4 and 5
lajocar is offline
View Public Profile Visit lajocar's homepage!
 
Old 11-05-2007, 12:51 AM Re: Differences between PHP4 and PHP5
bettytech's Avatar
Junior Talker

Posts: 4
Trades: 0
The "true" OO structure is one of the big points of PHP5. There are also a lot more built-in functions and several improvements.
The XSLT extension present in PHP4 is not the XSL extension in PHP 5.Additionally, the XML-DOM extension in PHP 4 is not the DOM extensionis PHP 5.Other than that, I didn't see a whole lot of difference either.
bettytech is offline
View Public Profile
 
Old 11-05-2007, 12:18 PM Re: Differences between PHP4 and PHP5
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,513
Name: Dan
Location: Swindon
Trades: 0
recently im getting more annouyed with my host who is STILL!! on Php4

someone wrote soemthing for me and it used the stripos function which is in php5 however i did a google and someone has made a function which did i aif not exis create it, thing which as far as i can tell basically made the function but its just annouying! i dont want to even try and learn oop because i know it will just cause trouble, as i either wont be able to use it or ill then have to remember to do stuff differant because of the changed >< its just stupid i wish they would bloody install both php4 and 5 i knew the orginal guy who ran it and i actually got him to upgarde but then he downgraded again because he messed up their billing script! just install both and have php4 fun on your account and php5 on mine! ¬.¬

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<
dansgalaxy is offline
View Public Profile Visit dansgalaxy's homepage!
 
Old 11-22-2007, 08:52 AM Re: Differences between PHP4 and PHP5
ericajoieake's Avatar
Super Talker

Posts: 129
Trades: 0
Thanks for the info Christopher, now I know the differences of PHP4 and PHP5!
ericajoieake is offline
View Public Profile Visit ericajoieake's homepage!
 
Old 01-19-2008, 06:07 PM Re: Differences between PHP4 and PHP5
carloncho's Avatar
Skilled Talker

Posts: 80
Name: Carlos
Trades: 0
Excellent article. I have a compatibility problem with include files. In a server with PHP4 a file '../../includes/file.php' works, but when i move to a php5 server, not. I have to change to '/var/www/includes/file.php', the complete path to file.
I dont know yet, if this is an incompatibility or an error from me
__________________
-----------------------
http://www.xumby.com
carloncho is offline
View Public Profile Visit carloncho's homepage!
 
Closed Thread     « Reply to Differences between PHP4 and PHP5

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.26132 seconds with 13 queries