Reply to Re: how to dynamically create member functions

Your name:

Reply:


Posted by Janwillem Borleffs on 10/10/38 11:17

maxlego wrote:
> I am trying to dynamically create a new member function to my class. I
> have come up with this piece of code. It almost looks as if it adds
> the member functions to the class. But when trying to call them,
> nothing happens. Is this possible at all?
>

Not the way you are trying to, but it is possible using the classkit
extension (http://www.php.net/classkit).

> I am trying to do this thing because all the mmeber functions would
> have similar functionality except names. Since there are no
> facilities like C preprocessor I thought that eval might help.
>

Sounds to me that a __call() method would meet your needs:

<?php

class X {
private $members = array();

function __construct() {
$this->members[] = 'foo';
$this->members[] = 'bar';
}

function lambda($x) {
print $x;
}

function __call($name, $arg) {
if (!in_array($name, $this->members)) {
throw new Exception("No such method: $name()");
} else {
call_user_func_array(array($this,'lambda'), $arg);
}
}
}

$a = new X();
$a->bar(11);

?>


JW

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация