TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Treat PHP Strings as Objects (Like Javascript) (http://www.talkphp.com/tips-tricks/4555-treat-php-strings-objects-like-javascript.html)

Wildhoney 06-15-2009 01:36 PM

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.

Salathe 06-15-2009 02:10 PM

Nifty, highly impractical (checking every global again and again!) but a fun concept nevertheless. :-)

sketchMedia 06-15-2009 02:34 PM

Interesting little idea there.

Tanax 06-15-2009 08:35 PM

I don't understand what the toObject thing does.. ?

Wildhoney 06-15-2009 08:39 PM

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.

Wildhoney 06-16-2009 12:37 AM

Who here thinks PHP should be this way naturally? That way all the functions will be removed from the main scope.

Enfernikus 06-16-2009 12:38 AM

I'd definitely love to see PHP work this way but we'll never see it. Ever.

ETbyrne 06-16-2009 01:01 AM

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!

Wildhoney 06-16-2009 10:38 AM

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.

Tanax 06-16-2009 03:53 PM

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);

Wildhoney 06-16-2009 04:36 PM

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.

Tanax 06-17-2009 12:02 PM

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!

ETbyrne 06-17-2009 08:31 PM

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.


Village Idiot 06-18-2009 05:24 PM

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.

sketchMedia 06-19-2009 01:34 PM

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.


All times are GMT. The time now is 08:02 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0