Recent Changes - Search:

aggregate

In PHP4, we have aggregate to simulate multiple inheritance (MI) without type information. PHP5 conveniently removes that function from the core without providing proper MI, so here's a workaround for PHP5 (which will also work in PHP4):

class Child extends Parent1 {
   var $constructor = true;
   function Child() {
      if (function_exists('aggregate')) {
         aggregate($this, 'Parent2');
         Parent2::Parent2($constructorArgs...);
      }
      else {
         $this->parent2 = new Parent2($constructorArgs...);
      }
      $this->constructor = false;
   }

   function __call($methodName, $args) {
      if (method_exists($this->parent2, $methodName)) {
         call_user_func_array(array($this->parent2,$methodName), $args);
      }
   }

   function __set($name, $val) {
      if ($this->constructor) {
         $this->{$name} = $val;
      }
      else {
         if (isset($this->parent2)) {
            $this->parent2->{$name} = $val;
         }
      }
   }

   function __get($name) {
      if (!$this->constructor) {
         return $this->parent2->{$name};
      }
   }

   /** Everything else ... **/
}

Simple, huh? (I haven't even generalised to multiple parents.) Ironically, this can't be made any simpler because there are no mixins, and class definitions aren't treated like procedural code . . . this kind of thing is enough to make me long for lisp. This kind of thing can be gotten around with the runkit extension, but, well, that's still just an extension.

OTOH, if someone knows of a simpler way, let me know.

Pass by pointer in arguments

In PHP 5, objects are passed by pointer rather than passing a copy (as was done in PHP 4). However, arrays are still passed by copy. Even (of course) if they are big multidimensional or associative arrays. Which is a shame, since I much prefer arrays/hashes for data. In fact, I'd prefer them for objects too, if that were syntactically feasible.

Cookies

setrawcookie() isn't entirely 'raw'. It will check the value for invalid characters, and then disallow the cookie if there are any. These are the invalid characters to keep in mind: ',;<space>\t\r\n\013\014'.

Note that comma, space and tab are three of the invalid characters. IE, Firefox and Opera work fine with these characters, and PHP reads cookies containing them fine as well. However, if you want to use these characters in cookies that you set from php, you need to use header().

Page last modified on December 19, 2006, at 04:32 PM
View Edit History Print Recent Changes Search