note 42397 added to function.is-scalar

From: Date: Fri, 14 May 2004 16:31:54 +0000
Subject: note 42397 added to function.is-scalar
Groups: php.notes 
Request: Send a blank email to php-notes+get-69617@lists.php.net to get a copy of this message
Hi ... for newbees here, I just want to mention that reference and scalar variable aren't the same. A reference is a pointer to a scalar, just like in C or C++. <? php // simple reference to scalar $a = 2; $ref = & $a; echo "$a <br> $ref"; ?> this should print out: "2 <br> 2". Scalar class also exists. Look below: <? php class Object_t { var $a; function Object_t () // constructor { $this->a = 1; } } $a = new Object_t; // we define a scalar object $ref_a = &a; echo "$a->a <br> $ref->a"; ?> again, this should echo: "1 <br> 1"; Here is another method isued in OOP to acheive on working only over reference to scalar object. Using this, you won't ever have to ask yourself if you work on a copy of the scalar or its reference. You will only possess reference to the scalar object. If you want to duplicate the scalar object, you will have to create a function for that purpose that would read by the reference the values and assign them to another scalar of the same type... or an other type, it is as you wish at that moment. <?php class objet_t { var $a; function object_t { $this->a = "patate_poil"; } } function &get_ref($object_type) { // here we create a scalar object in memory // and we return it by reference to the calling // control scope. return &new $object_type; } $ref_object_t = get_ref(object_t); echo "$ref_object_t->a <br>"; ?> this should echo: "patate_poit <br>". The only thing that I try to demonstrate is that scalar variable ARE object in memory while a reference is usualy a variable (scalar object) that contain the address of another scalar object, which contain the informations you want by using the reference. Good Luck! otek is popanowel HAT hotmailZ DOT cum ---- Manual Page -- http://www.php.net/manual/en/function.is-scalar.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42397 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42397&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42397&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69617) next »