Connecting Tech Pros Worldwide Help | Site Map

Does PHP have an equivalent of JavaScript's prototype object?

  #1  
Old November 22nd, 2005, 12:05 AM
alee.indy@gmail.com
Guest
 
Posts: n/a
Basically, I want to add default methods to the PHP string object so
that I can do something like:

$stringvar = "hello";

$stringvar -> append(" world"); // where append is some kind of
prototyped function i define

echo $stringvar; // "hello world"

is this at all possible?

thanks!!

  #2  
Old November 22nd, 2005, 12:05 AM
Erwin Moller
Guest
 
Posts: n/a

re: Does PHP have an equivalent of JavaScript's prototype object?


alee.indy@gmail.com wrote:
[color=blue]
> Basically, I want to add default methods to the PHP string object so
> that I can do something like:
>
> $stringvar = "hello";
>
> $stringvar -> append(" world"); // where append is some kind of
> prototyped function i define
>
> echo $stringvar; // "hello world"
>
> is this at all possible?
>
> thanks!![/color]

Hi,

AFAIK you cannot easily extend the existing String in PHP.
(You would have to dive into the sourcecode, modify it, and recompile)

But you can of course create your own wrapper-object.
Just create a new class MyString and extend it to your liking with methods
you like.

Regards,
Erwin Moller
  #3  
Old November 22nd, 2005, 12:05 AM
Colin McKinnon
Guest
 
Posts: n/a

re: Does PHP have an equivalent of JavaScript's prototype object?


alee.indy@gmail.com wrote:
[color=blue]
> Basically, I want to add default methods to the PHP string object so
> that I can do something like:
>
> $stringvar = "hello";
>
> $stringvar -> append(" world"); // where append is some kind of
> prototyped function i define
>
> echo $stringvar; // "hello world"
>
> is this at all possible?
>[/color]

No for two reasons:

1) PHP has primitive types which are not objects (strings, integers, floats,
arrays).

2) Unlike javascript, a PHP class cannot be modified at runtime.

Of course there is nothing to stop you creating a class and manipulating the
data in an instance using a function or a seperate class.

C.
  #4  
Old November 22nd, 2005, 12:06 AM
alee.indy@gmail.com
Guest
 
Posts: n/a

re: Does PHP have an equivalent of JavaScript's prototype object?


Thanks Erwin and Colin for the info. Hmm, is that one more reason to
switch to Ruby? =)

  #5  
Old November 22nd, 2005, 12:06 AM
Oliver Grätz
Guest
 
Posts: n/a

re: Does PHP have an equivalent of JavaScript's prototype object?


alee.indy@gmail.com schrieb:[color=blue]
> Basically, I want to add default methods to the PHP string object so
> that I can do something like:
>
> $stringvar = "hello";
>
> $stringvar -> append(" world"); // where append is some kind of
> prototyped function i define
>
> echo $stringvar; // "hello world"
>
> is this at all possible?[/color]

Surprise: There ain't even a string object!
PHP is all about speed through native code.
So strings are primitive types.

But for your example, as for most of the common string operations:
Do not reinvent the wheel! All common string manipulation functions are
already implemented as fast compiled C-code...

$stringvar='hello';
$stringvar.=' world';

OLLi
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.javascript FAQ - 7.9 - 2003-06-30 FAQ Poster answers 2 July 20th, 2005 09:51 AM