类中有一数组,本来是写死在里面的,但我想让它变活,先从外部查询数据表,然后再传进去,可是忙活一下午没成功。请教各位达人,怎么弄活它?注:此数组在类是一个成员属性,而非函数。
如下:
class menu {
public $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function __set(){ ..... }
... ...
}
如下:
class menu {
public $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function __set(){ ..... }
... ...
}
private $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function addLinks(array $links){
$this->links = $this->links + $links;
} public function getLinks(){
return $this->links;
}
}
class menu {
public $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function __set($name, $value){
this->links[$name] = $value
..... }
... ...
}
$menu->links = array(1,2,3);
public $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function __set($name = '', $value = ''){
$this->links[$name] = $value;
}
}
$menu = new menu();
$menu->__set('Home', 'abc');
老是忘标点
class Menu {
private $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function setLinks($sKey, $sLink){
$this->links[$sKey] = $sLink;
} public function getLinks($sKey){
return $this->links[$sKey];
}
}
$menu = new Menu();
$menu->setLinks('blog', 'blog.php');
print_r($menu);
echo $menu->getLinks('blog');
out:
Menu Object
(
[links:private] => Array
(
[Home] => index.php
[News] => news.php
[Products] => product.php
[Support] => support.php
[About] => about.php
[Contact Us] => contact.php
[blog] => blog.php
))
class menu {
public $links = array(
'Home' => 'index.php',
'News' => 'news.php',
'Products' => 'product.php',
'Support' => 'support.php',
'About' => 'about.php',
'Contact Us' => 'contact.php'
); public function __set($data, $other = ''){
$arr_keys = array_keys($data);
foreach($arr_keys as $k => $v){
$this->links[$v] = $data[$v];
}
}
}
$data = array(
'Home' => 'asdfdf',
'News' => 'bbbphp',
'Products' => 'aphp',
'Support' => 'bbbhp',
);
$menu = new menu();
$menu->__set($data);
__set是做什么,知道吗??
function __set($var_name){
//当实例化对象后,调用了不存在的对象属性时执行(赋值)
//$Object->not_var1 = "1";
if(isset($this->private_var[$var_name])) {
return($this->private_var[$var_name]);
}else{
return(NULL);
}
}