Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 22nd, 2005, 12:05 AM
alee.indy@gmail.com
Guest
 
Posts: n/a
Default Does PHP have an equivalent of JavaScript's prototype object?

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
Default 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
Default 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
Default 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
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.