Random things about objects..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pistacchio
Forum Newbie
Posts: 5
Joined: Wed Sep 01, 2004 5:59 am

Random things about objects..

Post by pistacchio »

hi to all!
i have a couple of newbie questions that are tantalizing me:
1. is there a way to do multiple operations (like variable assigaments and using functions) with an object without writing it fully over and over? Ex:

Code: Select all

$foo->color="violet";
$foo->name="flower";
$foo->scent="good";

// to become:
with $foo do {
 color="violet";
 name="flower";
 scent="goo";
}
2. once i've created an object from a class, can i add a function to that object that is not directly defined by the class? Ex:

Code: Select all

class foo {
 function bar() { }
}

$foobar=new foo;

//foobar only has the function bar(), but i want it to have a baar() function too
thanks in advance!
gustavo
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Random things about objects..

Post by Weirdan »

pistacchio wrote: 1. is there a way to do multiple operations (like variable assigaments and using functions) with an object without writing it fully over and over? Ex:

Code: Select all

$foo->color="violet";
$foo->name="flower";
$foo->scent="good";
// to become:
with $foo do {
 color="violet";
 name="flower";
 scent="goo";
}
Not in PHP4, not in PHP5 perhaps.
pistacchio wrote: 2. once i've created an object from a class, can i add a function to that object that is not directly defined by the class? Ex:

Code: Select all

class foo {
 function bar() { }
}

$foobar=new foo;

//foobar only has the function bar(), but i want it to have a baar() function too
no, but you can use variable variables or function handling functions to simulate such behavior:
  • variable variables:

    Code: Select all

    class a {
      var $b;
      function a($b=null) {
         $this->b = $b;
      }
      function some() {
         if(!is_null($this->b)&&is_callable($this->b)) {
             $callback = $this->b;
             $callback($this);
         }
         // do something....
      }
    }
    
    function cb($obj) {
       var_dump($obj);
    }
    $q = new a('cb');
    $q->some();
  • function handling functions:

    Code: Select all

    class a {
       var $callbacks = array();
       function a() {}
       function add_callback($cb) {
           $this->callbacks[] = $cb;
       }
       function some() {
          foreach($this->callbacks as $callback) {
              if(is_callable($callback))
                 call_user_func($callback, $this);
          }
       }
    }
    $q = new a;
    function e($obj) {
       var_dump($obj);
    }
    function f($obj) {
        echo "Class name is: ".  get_class($obj) . "\n";
    }
    $q->add_callback('e');
    $q->add_callback('f');
    $q->some();
pistacchio
Forum Newbie
Posts: 5
Joined: Wed Sep 01, 2004 5:59 am

THANKS!

Post by pistacchio »

i'll work on it, thank you so much :P
Post Reply