You are here: Re: Passing by Reference « PHP Programming Language « IT news, forums, messages
Re: Passing by Reference

Posted by C. on 09/11/07 12:53

On 11 Sep, 13:20, Sanders Kaufman <bu...@kaufman.net> wrote:
> I think I finally get the idea of passing by reference, but I'm
> struggling with the syntax and grammar of it.
>
> I've got an object and I want to send it to a function to have its
> properties modified like so:
>
> [code]
> class MyClass {
> var $a;
> function MyClass(){
> $this->a=0;
>
> }
>
> function fnClassify($oClass){
> $oClass->a = 1;
> return true;
>
> }
>
> $oMyClass = new MyClass();
> echo $oMyClass->a; // displays "0"
>
> $bSuccess = fnClassify($oMyClass);
> echo $oMyClass->a; // displays "1"
> [/code]
>
> Unfortunately, the syntax is wrong somehow. At some point, I should be
> passing a reference to, instead of the value of, $oMyClass - but I don't
> know where or how.
>
> Can someone help me out here?

I'm confused - do the comments show what you expect to happen? or what
*did* happen?

(the code you've posted shouldn't run - you're missing a '}' from the
method declaration).

IIRC PHP4 will pass the value of an object but PHP5 will pass a
reference to the object unless you specifically tell it to do
something different:

function fnClassify(&$oClass){

C.

 

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

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