![]() |
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:
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:
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:
php Code:
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. |
Nifty, highly impractical (checking every global again and again!) but a fun concept nevertheless. :-)
|
Interesting little idea there.
|
I don't understand what the toObject thing does.. ?
|
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.
|
Who here thinks PHP should be this way naturally? That way all the functions will be removed from the main scope.
|
I'd definitely love to see PHP work this way but we'll never see it. Ever.
|
Quote:
|
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.
|
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); |
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. |
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! |
Ok, here's a little String class I just whipped up:
PHP Code:
PHP Code:
Code:
This is Quixotic! |
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.
|
Quote:
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