|
Posted by gosha bine on 09/11/07 12:52
On 11.09.2007 14:20 Sanders Kaufman 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?
In php4 you should prepend & to any variable or a function parameter
containing or refering to an object. Examples include:
function foobar(&$someObject)
$someObj = &new ClassName;
$someObj = &$anotherObj;
Every function that returns an object, should return by reference (use &
twice)
function &getObject() {
....
return &$someObjectVar;
}
Again, all this is needed only for php4. I can't think of a good reason
to use it.
--
gosha bine
makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
[Back to original message]
|