есть ли способ заполнить массив в нутри класса т.е. к массиву с индексом 1 присваивалась переменная $la которая нахотися внутри класса. если есть способ напиши пожалуйста примерчик. PHP: class template{ var $file; function set_file_name($file) { $la = "kos"; echo $file['1']; } } $template = new template(); $template->set_file_name(array('1'=> $la));
Почему бы не сделать так? Код (Text): <?php class template{ var $file; var $la = "kos"; function set_file_name($file) { echo $file['1']; } } $template = new template(); $template->set_file_name(array('1' => $template->la)); ?>
PHP: <?php class template{ public $file = array(); protected $variable = ''; function __constructor(){ $this->file[] = $this->variable; } } $template = new template; ?> only PHP5
Значение переменной должно присваиваться в конструкторе класса, или быть константой (к классам имеющим опосредованное отношение) Я не прав? (Я старый, очень старый - в моей юности было именно так
Первый вариант: передача переменной по ссылке: PHP: <?php class example { function my(&$value) { $la = 'new value'; $value = $la; } } $myvalue = 'old value' $myclass = new example(); echo $myvalue; // output: old value $myclass->my($myvalue); echo $myvalue; // output: new value ?> Второй вариант: возврат значений: PHP: <?php class example { function my() { return 'new value'; } } $myvalue = 'old value' $myclass = new example(); echo $myvalue; // output: old value $myvalue = $myclass->my(); echo $myvalue; // output: new value ?>
вобщем вот мой класс надо Код (Text): $array = array("{NAME}" => $f['name'], "{ID}" => $f['id']; вынести за пределы класса но переменные $f['name'] и $f['id'] принимали значения так как еслибы они были внутри класса Код (Text): <?php class template{ var $tm_file; function assign_block_for($name, $count, $result, $array) { $string = $this->tm_file; $str1 = strpos($string,"<!-- FOR ".$name." -->"); $str2 = strpos($string,"<!-- END_FOR ".$name." -->"); $str3 = strlen("<!-- END_FOR ".$name." -->"); $str = $str2+$str3-$str1; $string_c = substr($string, $str1, $str); for ($i=0; $i < $count; $i++){ $f = mysql_fetch_array($result); $r = fmod($i, 2); if ($r==0){$style="lay";}else{$style="lay1";} $array = array("{NAME}" => $f['name'], "{ID}" => $f['id']; $string_e = $string_e.$string_c; $string_e = str_replace(array_keys ($array), array_values($array), $string_e); } $this->tm_file = substr_replace($string, $string_e, $str1, $str); } } $template = new template(); ?>
Задача, если честно, не ясна. Если тебе нужен $array вне класса, так верни его по ссылке в основной код. Если тебе надо его наполнять из массива $f вне класса тогда: PHP: <?php class template{ var $tm_file; var $f function assign_block_for($name, $count, $result) { $string = $this->tm_file; $str1 = strpos($string,"<!-- FOR ".$name." -->"); $str2 = strpos($string,"<!-- END_FOR ".$name." -->"); $str3 = strlen("<!-- END_FOR ".$name." -->"); $str = $str2+$str3-$str1; $string_c = substr($string, $str1, $str); for ($i=0; $i < $count; $i++){ $this->f = mysql_fetch_array($result); $r = fmod($i, 2); if ($r==0){$style="lay";}else{$style="lay1";} $string_e = $string_e.$string_c; $string_e = str_replace(array_keys ($array), array_values($array), $string_e); } $this->tm_file = substr_replace($string, $string_e, $str1, $str); } } $template = new template(); $array = array("{NAME}" => $template->f['name'], "{ID}" => $template->f['id']); ?>