NYCPHP Meetup

NYPHP.org

[nycphp-talk] Overriding Array's

Rob Marscher rmarscher at beaffinitive.com
Thu May 3 21:37:15 EDT 2007


On May 2, 2007, at 1:52 PM, Joseph Crawford wrote:
> We are looking to have the second array override the first array
> values.  I cannot seem to get any of these methods to
> work.

I assume you've fixed this by now... but here's a recursive method  
that I think does what you want:

// overrides non-numeric keys recursively
public function override($default, $page)
{
     foreach ($page as $key => $val)
     {
         if (isset($page[$key]) && $page[$key] !== "") {
             if (is_array($val)) {
                 $default[$key] = $this->override($default[$key],  
$page[$key]);
             } else {
                 if (is_numeric($key)) {
                     $default[] = $val;
                 } else {
                     $default[$key] = $val;
                 }
             }
         }
     }
     return $default;
}

Though, I wonder if it would be better to have these properties as  
class members... you may find that a bit more flexible... could even  
still have an override method to pass in an array... even offer it  
through the constructor... see below:

/* note: php4 syntax, I don't use php5 enough daily to spit it off  
the top of my head
  * but I think if you search/replace var for public and add public  
in the class definition and
  * to the functions and make singleton a static method, that should  
be most of it
  */
class Config
{
   // http-metas
   var $content-type = 'text/html; charset=utf-8';
   var $content-language = 'en-us';

   // metas
   var $PreventParsing = true;
   var $robots = 'noindex, nofollow';
   var $description = 'Recruiting and HR news, information and '
                     . 'community -- including articles, discussions,  
blogs, jobs,
                     . 'conferences, research, email publications and  
more.'
   var $copyright = '2007';
   var $keywords = 'recruiting, staffing, HR, business';

   // page
   var $template = 'mainTemplate';
   var $title = 'ERE.net - Recruiting news, information and community';
   var $ssl = 'false';

   // links
   var $stylesheets = array('style.css', 'layout.css');

   function Config($override = array())
   {
     $this->__construct($override);
   }

   function __construct($override = array())
   {
     $this->override($override);
   }

   function singleton($config = array())
   {
     static $instance;
     if (!isset($instance)) {
       $instance = new Config($config);
     }
     return $instance;
   }

   function override($values = array())
   {
     foreach($values as $key => $value) {
       if (is_array($this->$key)) {
         $this->$key[] = $value;
       } else {
         $this->$key = $value;
       }
     }
   }

   function getHttpMetas()
   {
     return array('content-type' => $this->content_type,
                           'content-language' => $this->content- 
language);
   }

   function getMetas()
   {
     return array('content-type' => $this->content_type,
                           'content-language' => $this->content- 
language);
   }

   function getPage()
   {
     return array('template' => $this->template,
                           'title' => $this->title,
                           'ssl' => $this->ssl);
   }

   function getLinks()
   {
     return array('stylesheets' => $this->stylesheets);
   }
}





More information about the talk mailing list