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