note 14303 deleted from function.list by aidan
| From: | aidan@php.net | Date: | Sat, 14 Aug 2004 20:20:00 +0000 |
| Subject: | note 14303 deleted from function.list by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74645@lists.php.net to get a copy of this message | ||
Note Submitter: x@x
----
Think of list() as if it was a function building and returning a non-associative positional array of
variable references from its arguments, the latter arguments being prototyped as reference
arguments. The returned array is then returned by list() also as a reference instead of as a value
(as with classic scalar functions)
Then think of = being a standard assignment operator working on arrays. The difference between this
assignment and other scalar array assignments is that:
- when the assigned right array is a reference to a non-associative array,
- and that right array contains variable references instead of values
- then the left array of values is not being copied to the left array, but instead scanned
positionally to assigned each variable reference specified in the left array.
One way to make PHP more general to support further use of list() should be that an array can
contain either (possibly associative) values and no references, or either references and no values.
This restriction creates a distinct type of array, that internally could be managed as a separate
datatype (datatype "List" instead of "Array").
With this extension, PHP could handle the notion of references, and could conveniently represent
complex graph structures, as those manipulated in Lisp, or those built in K&R C using pointers,
or in Ansi-C or C++ using references.
For this extension to work effectively, the syntax of functions should be modified so that one could
also specify reference parameters:
function sample(& $ref1, & $ref2) {
return array($ref1, $ref2);
}
This sample would clearly create a new datatype, because above $ref1 and $ref2 have no scalar
datatype, but instead a polymorphic "Ref" datatype.
However sample() could dereference the variables $ref1 using scalar operators such as "+":
- the "&" presence in arguments definition forces any use of the function to pass its
corresponding arguments by reference instead of by value.
- The datatype() builtin function should still return the base datatype name of the referenced
variable if a value was assigned to it. This could be used in sample() to test how to use the
referenced variable, the same way we do it with classic scalar variables.
- there should be a new builtin to test if a variable is a reference: isref($ref1) would return true
in sample() above, or even within standard functions called within sample() in which $ref1 is passed
as actual parameter. There should not be any modification to those functions, which still could
dereference the parameter (the main difference being that assigning a value to a parameter for which
isref() returns true would modify the variable passed by the calling function).
- list() can be used with optional references, so should any function with ref parameters. One way
to manage it would be that list() could skip some indices within the returned constructed array of
references (so that isset() will return false on missing indices)
- a function specifying a ref parameter could test that a reference was actually passed by using
isset()
- functions defined with variable argument lists could also work with the $arg predefined array:
arguments list is an associative array that may contain references and values that could be tested
by isset() and isref()
- code using a function declared to accept ref parameters could still avoid passing the reference to
the actual variable by duplicating it temporarily for example: <? sample(new $var1); ?>
- a function specified with a ref parameter could be called with an expression by the same technic:
<? sample(new 1+2); ?>
- a function specified with a ref parameter should not be callable with a scalar argument (an
exception should occur before call if the declaration is explicitly showing a ref value), such that
<? sample(1+2); ?> would raise a compile error.