[thelist] another php question: object reference syntax

Tim Parkin tim at pollenation.net
Wed Jul 17 16:34:00 CDT 2002


>So - I CAN use dot syntax, but if I make a pointer, I have to use ->
>correct?

If you check the manual you'll see the . is a string concatenation
operator so if you try

$var = 1;
$test = "terry";

$var.test = 7; // you'll get an error saying unexpected '=' (it's on the
wrong side of a construct

$temp = $var.test; // will assign "1terry" to $temp

${$test.var} = "manwell" // will assign "manwell" to $terryvar

unless you put this before it

define(var,"totty");

in which case

${$test.var} = "manwell" // will assign "manwell" to $terrytotty

This is because it will presume var is a defined value and if it can't
find one to match it it will assign the defined value to it's own string
(in this case "var")

To confuse you a bit more

$terry = "tim";
define("terry","tony")

${$terry.terry) = $terry.$terry;

assigns the value "timtim" to $timtony;

so $timtony=timtim and $terry=tim and terry=tony and
$terry.terry=timtony

Tim

Ps terry::tim could well equal $tony->tim if $tony = new Terry and <?php
class Terry { function tim() { return TRUE; } } ?>

Pps :: means you can call functions from classes without having to
instantiate a class .. very much like perl, on which php was mostly
based, which itself inherits some of it's syntax from C. You'd best
blame all of the those damned closed source perl coders for shoddy
syntax..








More information about the thelist mailing list