note 102865 added to language.types.array

From: Date: Fri, 11 Mar 2011 00:54:32 +0000
Subject: note 102865 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-177782@lists.php.net to get a copy of this message
// Sometimes you may need an array of constants including concatenated strings. // - That works fine in this case: define("GLUE", "&"); function array_to_string($a) { $r = ""; foreach ($a as $key => $value) { $r .= "$key => $value<br>"; }; return $r; } // DECLARING ARRAY (1) $array = array("one", "two", "one".GLUE."two"); echo array_to_string($array); // - This seems rather strange behavior in a CLASS nevertheless: class wrong_class { // DECLARING ARRAY (2) private $array = array("one", "two", "one".GLUE."two"); function __toString() { return array_to_string($this->array); } } $wc = new wrong_class(); echo $wc; /* Parse error: parse error, expecting `')'' in ... code on line 21 */ // - Anyway, if you force this declaring method inside a CLASS, you could change the object map directly: class proper_class { function __construct() { // DECLARING ARRAY (3) $this->array = array("one", "two", "one".GLUE."two"); } function __toString() { return array_to_string($this->array); } } $pc = new proper_class(); echo $pc; ---- Server IP: 64.71.164.2 Probable Submitter: 84.224.70.0 ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- https://master.php.net/note/edit/102865 Del: integrated -- https://master.php.net/note/delete/102865/integrated Del: useless -- https://master.php.net/note/delete/102865/useless Del: bad code -- https://master.php.net/note/delete/102865/bad+code Del: spam -- https://master.php.net/note/delete/102865/spam Del: non-english -- https://master.php.net/note/delete/102865/non-english Del: in docs -- https://master.php.net/note/delete/102865/in+docs Del: other reasons-- https://master.php.net/note/delete/102865 Reject -- https://master.php.net/note/reject/102865 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177782) next »