radalin Posted July 4, 2007 Share Posted July 4, 2007 Hi, I'm curious if anyone knows such thing is possible or if this is possible how it can be done. I mean that you call a function, which is not defined using "function xxx" but by someother way, then when we recognize that the function we have called is in our function list array, then it calls a generic function which takes as argument some part of the function that exists in the function list. What I mean is something like this: <?php $functionList = array("myfunction1","myfunction2","myfunction3"); function myfunctionX($id,$X) { echo $id*$X . "<br />"; } //....Code that check if the function name exists int the functionlist array... myfunction1(10); //Output: 10 myfunction2(10); //Output: 20 myfunction3(10); //Output: 30 ?> When myfunction1 is called, we check that if it exists in the functionlist array. Then if it exists we call the generic myfunctionX() and send the number (myfunction1 => 1, myfunction2 => 2) as an argument of myfunctionX() Well I hope it was clear for you to understand. I'm not sure how it's possible, it seems to me as a php hack but I'm not quite very sure. I appreciate if you show me some clues. Thanks for your time. Note: It's not only about begin lazy to write functions Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 4, 2007 Share Posted July 4, 2007 I cant really think of anyway to do what your after; and i really cant see the point of it either. Perhaps if you showed where you were actually planning on using a technique like this, someone might be able to suggest a way of doing it? Quote Link to comment Share on other sites More sharing options...
radalin Posted July 5, 2007 Author Share Posted July 5, 2007 Well ok. Perhaps I should change the explanation type. Let's say we have a class with some attribute: x,y,z,k. I have to write get and set method for them if I define them private. Let's say that I did so. Now I have to do something like this: <?php get_k() { return $this->k;} get_x() { return $this->x;} get_y() { return $this->y;} get_z() { return $this->z;} set_k($val) { $this->k = $val;} set_x($val) { $this->z = $val;} set_y($val) { $this->y = $val;} set_z($val) { $this->z = $val;} ?> As you see these methods are doig approximately the same thing. I can something like this instead: <?php get($key) { return $this->$key; } set($key,$val) { return $this->$key = $val; } ?> This may be written wrong if you think the syntax but you get the idea I think. But in this type of writing get_x() is not defined. I want that when I call get_x() it returns the value of get($key). Yes of course I may use this: <?php get_x() { return $this->get("x"); } set_x($val) { $this->set("x",$val); } ?> It's ok but I have to write and define them. It's not about begin lazy to define them. It's about easy management. In this case inside of the function may not change a lot but with something a little complex things get boring with finding which functions are changed or not and which one must be changed. Instead of this kind of using. <?php $getfunctionlist = array("get_x","get_y","get_z","get_k"); $setfunctionlist = array("set_x","set_y","set_k","set_z"); //Something comes here to check if the above functions are called... //and which that functions will call the below functions get($key) { return $this->$key; } set($key,$val) { $this->$key = $val; } ?> Why do I need this? Well it's about being a little lazy too of course but mostly, I was curious if such a thing can happen. Thanks again for your time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.