TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 06-15-2009, 01:36 PM   #1 (permalink)
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)
 



Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB 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
Easily Format JSON using PHP and Interpret using Javascript Wildhoney Advanced PHP Programming 13 02-01-2013 11:05 AM
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM
Preventing Spam with PHP and Javascript Wildhoney General 9 10-24-2007 11:35 AM


All times are GMT. The time now is 04:01 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design