PHP: <? $arr = array ( "a" => "b", "c" => "d" ); define("SOME_CONST",serialize($arr)); var_dump(unserialize(SOME_CONST)); так все нормально... PHP: <?php $arr = array ( "a" => "b", "c" => "d" ); define("SOME_CONST",serialize($arr)); class A { private $asd = unserialize(SOME_CONST); } ?> а так нет, почему?
Потому как правильно вот так: PHP: <? class A { private $something; public function __construct($ob) { $this->something = unserialize($ob); } }; ?> http://ee.php.net/manual/en/language.oo ... erties.php
config.php PHP: <?php define("DEFAULT_USER_MESSAGE", ""); define("DEFAULT_USER_NAME", "Unknown"); ?> parser.php PHP: <?php class Parser { protected $user_name = DEFAULT_USER_NAME; protected $user_message = DEFAULT_USER_MESSAGE; protected $smiles_array = array(";)" => "img/smiles/smile.gif", ":)" => "img/smiles/gentel.gif", ":]" => "img/smiles/pray.gif" ); public function __construct($name,$message) { if($name != ''){ $this->user_name = $this->prepare($name); } if($message != ''){ $this->user_message = $this->prepare($message); } } protected function prepare($text){ return htmlspecialchars($text); } public function set_smiles(){ foreach ($this->smiles_array as $key => $value ){ $this->user_message = str_replace($key, "<img src='".$value."'>", $this->user_message); } } public function build_string(){ return '<span class="chat_string">'.'['.date("H:i:s").']'.'['.$this->user_name.'] '.$this->user_message.'</span>'; } } ?> вот что получается PHP: <?php include("core/modules/config.php"); include("core/class/parser.php"); $message = new Parser('vasia','ololo pish pish :) '); $message->set_smiles(); echo $message->build_string(); ?> хотел просто в конфиг вынести массив со смайликами... как в таком случае делать? если все при создании объекта передавать в __construct , ну не знаю как то это некрасиво будет, переделаю конечно если так неправильно зы: сильно не пинайте, мой первый класс ^^
как то так? PHP: <? class Parser { /** * Имя пользователя. * @var string */ protected $user_name = null; /** * Сообщение пользователя. * @var string */ protected $user_message = null; /** * Массив со смайлами и путями к ним. * @var array */ protected $smiles_array = array(); /** * Конструктор */ public function __construct($name,$message,$default_username,$smiles,$name_length = 12,$message_length = 60) { $this->smiles_array = $smiles; if($name != ''){ $this->user_name = $this->prepare($name,$name_length); } else { $this->user_name = $default_username; } if($message != ''){ $this->user_message = $this->prepare($message,$message_length); } } /** * Удаление нежелательных символов и обрезание * до нужной длинны * @param string $text переменная для обработки * @param string $length максимальная длинна переменной. * @return string */ protected function prepare($text,$length){ $text = substr($text, 0, $length); return htmlspecialchars($text); } /** * Заменяет подстроку на картинку смайлика * * @return string */ public function set_smiles(){ foreach ($this->smiles_array as $key => $value ){ $this->user_message = str_replace($key, "<img src='".$value."'>", $this->user_message); } } /** * Формирует строку сообщения * * @return string */ public function build_string(){ return '<span class="chat_string">'.'['.date("H:i:s").']'.'['.$this->user_name.'] '.$this->user_message.'</span>'; } } ?>