You are here: Re: [PHP] call a function within the same class « PHP « IT news, forums, messages
Re: [PHP] call a function within the same class

Posted by Richard Lynch on 01/06/05 18:57

kalinga wrote:
> Dear all,
> I recently started PHP OOP and I'm bit confused about best and the most
> efficient methods when 'declaring a class' and 'calling function',
> could somebody
> explain me with following sample code, it would be great..
>
> thanks..
>
> class classLdap{
>
> $rslt = $_POST['rslt'];
>
> function ldapConnect($rslt){
> ....
> ......
> return $rslt;
> }// end function ldapConnect
>
> function ldapAdd($rslt){
> // i want to call ldapConnect($rslt) here what is the best
> method.
>
> $rslt = classLdap::ldapConnect($rslt);

This is generally done when you:
A) Don't have an instance of a classLdap to work with.
B) Calling ldapConnect() on the instance you have would cause side effects
to the instance (or other objects) that you don't want to happen in some
unusual case.

A. does not apply here, as you are in the method of the class, so you have
'$this' which is the instance you have created.

B. might or might not apply, but it all depends on YOUR application and
what you want it to do...

> //or
> //(curently i'm doing this way, it's to lengthy)
>
> $new_classLdap = new classLdap;
> $rslt = $new_classLdap->ldapConnect($rslt);

This can also be used to avoid altering the existing object you have
created -- though it's a bit more expensive than the previous way of doing
that.

There might be some super RARE case where you really really need an object
instantiated to have it be valid, and you would *HAVE* to do the above.

But that would be super rare, so this is probably not what you want.

> //or
>
> $rslt = $this->ldapConnect($rslt);


works if you just want to affect *THIS* same object that you have -- In
other words, use $this-> when you are thinking about 'this' object that
you have created and want it to change itself in some way. Kind of like a
"self-help" sort of deal.


You might not want to assign the result back into $rslt -- depending on
what $rslt actually *IS* which I don't know, since you have it coming in
from $_POST...

Which you also need to do some checking on, since it's NOT SAFE to accept
$_POST data -- *any* Bad Guy out there could send any nasty thing they
want to your script through $_POST.

> }// end function ldapAdd
>
> }// end class

Based on what you are doing, and your experience level, I'd say stick with
the last answer:

$this->ldapConnect($rslt)

unless you have a very specific need to use the others.

Just keep it in the back of your mind that there *ARE* alternatives, so
that, years from now, when you run into one of those weird-o cases where
you need the others, you'll remember that they exist as alternatives.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация