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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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,215
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)
Old 06-15-2009, 02:10 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Nifty, highly impractical (checking every global again and again!) but a fun concept nevertheless.
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 06-15-2009, 02:34 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

Interesting little idea there.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-15-2009, 08:35 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,053
Thanks: 115
Tanax is on a distinguished road
Default

I don't understand what the toObject thing does.. ?
__________________
Tanax is offline  
Reply With Quote
Old 06-15-2009, 08:39 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

If loops through all the variables in the current scope, and excludes those that are not strings. If it finds variables that contains strings, then that variable is converted to an object.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
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
Old 06-16-2009, 12:37 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Who here thinks PHP should be this way naturally? That way all the functions will be removed from the main scope.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
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
Old 06-16-2009, 12:38 AM   #7 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 322
Thanks: 2
Enfernikus is on a distinguished road
Default

I'd definitely love to see PHP work this way but we'll never see it. Ever.
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 06-16-2009, 01:01 AM   #8 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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

PHP Code:
$pString = new String('I Love TalkPHP');
echo 
$pString
Are you kidding!? I think that's a great idea!
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 06-16-2009, 10:38 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Heh! Yes, you could actually work it that way, and then use it the way I demonstrated. It's just in my example, I wanted to work it so that the conversion of strings to objects were masked, but that of course is a more practical way of doing it, if you really do prefer the OOP way.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
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
Old 06-16-2009, 03:53 PM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,053
Thanks: 115
Tanax is on a distinguished road
Default

Oh, I understand.. so all the strings are passed on to the __toObject function via the "register" -thing.

Though, I don't understand what this does:
declare (ticks = 1);
__________________
Tanax is offline  
Reply With Quote
Old 06-16-2009, 04:36 PM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

That's right. The __toObject function could be anything, and doesn't even need to be a part of the class itself.

Most people haven't heard of the ticks functionality. What it does is allows you to make a call to a function periodically. The ticks, essentially, relates to a line. So a tick of 1 would make a call to the registered tick function every line, and a tick of 2 every 2 lines.

Good for memory management, as I described in this thread.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
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
Old 06-17-2009, 12:02 PM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,053
Thanks: 115
Tanax is on a distinguished road
Default

Ahh! So it checks every line if it contains a String, and passes it to the registered function.

I think I get it now. Very useful Thanks!
__________________
Tanax is offline  
Reply With Quote
Old 06-17-2009, 08:31 PM   #13 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Ok, here's a little String class I just whipped up:

PHP Code:
class String
{
    private 
$string;

    public function 
__construct($data=''){$this->string $data;}
    public function 
__toString(){return $this->string;}
    
    public function 
set($data=''){$this->string=$data;return $this;}
    public function 
append($data=''){$this->string.=$data;return $this;}
    public function 
prepend($data=''){$this->string=($data.$this->string);return $this;}
    public function 
lowercase(){$this->string=strtolower($this->string);return $this;}
    public function 
uppercase(){$this->string=strtoupper($this->string);return $this;}

And here is and example of using it:

PHP Code:
$test = new String('This is Quixotic!');
echo 
$test;
echo 
'<br />';

echo 
$test->lowercase()->append(' Wow!');
echo 
'<br />';

echo 
$test->prepend('Very Nice! ');
echo 
'<br />';

echo 
$test->set('Starting over.'); 
That would output this:

Code:
This is Quixotic!
this is quixotic! Wow!
Very Nice! this is quixotic! Wow!
Starting over.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 06-18-2009, 05:24 PM   #14 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

I don't see the point in treating strings as objects. It takes longer to process and serves no useful purpose whatsoever. This looks like a cheap attempt at how .NET does strings.
Village Idiot is offline  
Reply With Quote
Old 06-19-2009, 01:34 PM   #15 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

Quote:
This looks like a cheap attempt at how .NET does strings.
And JavaScript/Java/Ruby etc etc.

The point as I see it is to try and fudge PHP into being a proper OO language (and by that I mean that everything is considered an object not just an OO layer fudged on the top of an existing language *cough*C++*cough*).

I agree for PHP it mostly has no practical use, apart from adding more overhead. However it is an interesting way of showing what can be done.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 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 9 06-22-2009 01:12 AM
10 PHP Myths Dispelled Wildhoney General 10 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 04: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 11:53 PM.

 
     

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