Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-18-2006, 01:26 PM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
Unleash the power of PHP 5

Ok many people are sceptic about PHP 5, many noticed bugs and issues and others unleash the new powers.

WARNING this article is NOT for the PHP savvy who just got his hands on a book like "PHP for dummies". This article explains a big advantage of PHP 5 so you must be familiar with objects and variable types.

First all the code:
PHP Code:
class Readonly
{
    private 
$data# Read only properties
    
public function __construct($data)
    {
        
$this->data $data;
    }
    public function 
__get($key)
    {
        if (isset(
$this->data[$key])) { return $this->data[$key]; }
        
trigger_error('Undefined property: '.$key);
        return 
null;
    }
    public function 
__set($key$val) { throw new Exception('Disallowed to set property: '.$key); }
    public function 
__isset($key) { return isset($this->data[$key]); }
    public function 
__unset($key) { throw new Exception('Disallowed to unset property: '.$key); }
    public function 
get_data() { return $this->data; }

LOL you thought it was big

The class name should already explain what it does. It makes the data inside to be read-only so that a programmer can't overwrite the data.
Why? Well simple, who has ever write code and forgot the second '=' in an expression where it should have been '=='?
Right we all do, and its a pain to track the bug down. Sometimes you don't even notice it

Cool, now tell me, how does it work......
PHP Code:
// The data
$data = array(
    
'var1' => 'value 1',
    
'var2' => 'value 2',
    
'var3' => 'value 3',
);
// Make it read-only
$data = new Readonly($data);

// Read a property
echo 'Value of var1 is: '.$data->var1;

// Test if we can change the value
$data->var1 'foobar'// exception thrown 
Thanks to PHP 5 we now can eliminate some bugs we normaly produce, because every time we try to set a property the script dies on us.
This is something which is NOT possible in PHP 4 and a bug like:
PHP Code:
if ($data['var1'] = 'foo')
{
  
// execute

happens all the time.

I haven't explained bugs but if you all like to know more about this then i will write a follow about the above code including more examples and known bugs.
DJMaze is offline   Reply With Quote
Old 06-18-2006, 02:21 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Ack, I hate the fact you can do that in PHP. I love the improved object support in PHP, but I'm always going to think of weak-typing as a design flaw (declaring data as an array AND and object).

Another thing I'd love to see in PHP is Threads. There's fork(), but that's really not a good way to work in a concurrent environment, not on the level with pthreads in C and Threads in Java.

The implicit methods __set, __get, etc are neat - I hadn't thought of using them in this fashion (in fact, I hadn't given them much thought before), but I'll give it a shot.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-18-2006, 02:38 PM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
__set, __get, etc. have a bug "by design"
PHP Code:
class a
{
    private 
$data = array();
    public function 
__set($key$val)
    {
        echo 
"a sets $key to $val";
        
$this->data[$key] = $val;
    }
}

class 
b
{
    public function 
foo()
    {
        echo 
$this->val;
    }
}

$a = new a();
$a->= new b();
$a->b->'foobar'// error 
That's why my above script puts objects in public and variables in the protected $data.

Same bug happens on arrays
PHP Code:
$a = new a();
$a->= array('entry');
echo 
$a->b[0]; // error 
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a search engine for your website with PHP zhisede PHP 3 10-25-2005 07:54 AM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
PHP to Flash control panel roccoman PHP 1 11-24-2004 08:10 AM
PHP vs .NET Redline Lounge 1 11-24-2004 06:10 AM
I need to learn PHP Nitro PHP 9 06-28-2003 11:24 AM


All times are GMT -8. The time now is 08:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting