View Single Post
Old 06-15-2009, 01:36 PM   #1 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile Treat PHP Strings as Objects (Like Javascript)

I had a thought this morning whether or not it'd be possible to treat a PHP string as an object, but still initiate the variables the same way.

With a little spare time today I managed to find a way to do it. I much prefer the JavaScript way where most things are objects. I dislike PHP's way of function($szVariable), in JavaScript it would be something such as szVariable.function().

Using this method would be too easy, and something I wanted to avoid:

php Code:
$pString = new String('I Love TalkPHP');
echo $pString;

However, I've managed to create it in a way where the following is possible just via PHP. You still deal with the creation of the strings the same way, but it differs in that all strings will now be objects and so interacted with in a different fashion.

php Code:
$szVariable = 'I Love TalkPHP';
$szVariable->toLowerCase();
echo $szVariable;

Now I am not claiming this to be a good solution, because in actuality it is probably going to be somewhat slow, especially in larger applications. However, it is a nice trick for you just because !

By using the declare function we can traverse through all the variables in the current project, detect if they're strings, and if they are convert them to objects. That is the inner workings in a nutshell. It really is that simple.

Below you can see the code. Hopefully you will be able to work out what's happening, but using this method of working, you can do just about anything.

Bravo ticks!

php Code:
class String
{
    private $m_szText;
   
    public function __construct($szText)
    {
        $this->m_szText = $szText;
    }
   
    public function toLowerCase()
    {
        $this->m_szText = strtolower($this->m_szText);
    }
   
    public function __toString()
    {
        return $this->m_szText;
    }
   
    public static function __toObject()
    {
        foreach ($GLOBALS as &$pNode)
        {
            if (!is_string($pNode))
            {
                continue;
            }
           
            $pNode = new self($pNode);
        }
    }
}

php Code:
register_tick_function(array('String', '__toObject'));
declare (ticks = 1);

$szVariable = 'I Love TalkPHP';
$szVariable->toLowerCase();
echo $szVariable;

Using the magic method __toString, we can even give the illusion that it's not an object. Now you can treat PHP strings just as if they were JavaScript strings.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 06-15-2009 at 02:19 PM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
sketchMedia (06-15-2009)