类中有一数组,本来是写死在里面的,但我想让它变活,先从外部查询数据表,然后再传进去,可是忙活一下午没成功。请教各位达人,怎么弄活它?注:此数组在类是一个成员属性,而非函数。
如下:
 
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(){        .....    }
    ... ...

解决方案 »

  1.   

    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 addLinks(array $links){
          $this->links = $this->links + $links;
       }   public function getLinks(){
          return $this->links;
       }
    }
      

  2.   


    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
            .....    }
        ... ...
    }
      

  3.   

    $menu = new menu();
    $menu->links = array(1,2,3);
      

  4.   

    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 = new menu();
    $menu->__set('Home', 'abc');
    老是忘标点
      

  5.   


    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
            ))
      

  6.   

    这个意思????
    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);
      

  7.   


    __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); 
    }
    }
      

  8.   

    类似java,的确有你说的那个功能,不过它也是用来干这个的,虽然有点多此一举
      

  9.   

    嘿嘿,这个正确的,__set干别的的确不太适合