Page 1 of 1

Assigning a function to a variable...

Posted: Fri Sep 21, 2007 11:04 am
by kendall
I wasnt sure how to phrase this question nore the topic...

but i am developing a class in which i developed a method that i want to consider as a "default function"

now how i want this class to work is that you can create a "user defined function" and assign it as a "class function"

thus i have something like

Code: Select all

class OOP{
var $default_func = "default_func"

function default_func (){
}

}
you would have then used it like

Code: Select all

function my_func(){
}
$a = new OPP();
$a->default_func = 'my_func';
echo $a->default_func();
can this be done...i'm researching Object Oreiented Programming but im not sure whats the term used for this kind of thing

Kendall

Posted: Fri Sep 21, 2007 11:09 am
by John Cartwright
call_user_func() might be of interest

But seriously, explain more please as this seems like a nasty code smelling goblin.

Posted: Fri Sep 21, 2007 11:20 am
by kendall
Jcart wrote:call_user_func() might be of interest

But seriously, explain more please as this seems like a nasty code smelling goblin.
what do you mean?

i want the ability of someone to use my class without having to actually alter the class code itself but just create a function outside its scope and have the class use it as one of its method

i have done this in actionscript for flash but never seen or heard it done in php...but then again...im not good at OOP in php :oops:

Posted: Fri Sep 21, 2007 11:29 am
by kendall
i saw someone do a

Code: Select all

$func = ($img == "jpeg")? "createjpeg" : "createpng";

$func($img);
and it works so i was just thinking it wud work in a class scenario

Posted: Fri Sep 21, 2007 11:37 am
by Zoxive
Im pretty sure its not possible Fully, (Private/Protected Vars/Functions) Unless you pass all Variables by Reference, but then you still can't use the protected/private functions.

I was messing around with this last week, but haven't touched it since, phpFire events, keep in mind, it is Per Class and not per object (I used it for singleton classes).



To explain this further.

What he wants to do is He is making A class, and wants to be able to add Functions to it on the fly.

Code: Select all

class Test{
  $Var = 'hi';
  function say(){
    echo $this->Var;
  }
}

Function Test_add(){
  $this->Var = 'Hello';
}
Basically Add Test_add() as if it belonged to Class Test, so it could access all the Variables/Functions as Its Self.

Posted: Fri Sep 21, 2007 11:42 am
by CoderGoblin
Jcart wrote:nasty code smelling goblin.
Hey I think you're cute too. :lol:

Posted: Fri Sep 21, 2007 12:05 pm
by pickle
Why not just allow the user to extend the class?

Posted: Fri Sep 21, 2007 12:09 pm
by kendall
pickle wrote:Why not just allow the user to extend the class?
standardization i guess in the code

i just wanted to have one standard class and not have to be extending it everytime i want to just add a function

Posted: Fri Sep 21, 2007 12:10 pm
by John Cartwright
__call() and call_user_func_array() seems appropriate. If you need the ability to have these user defined function access the current scope then you'll need to extend the object. However, I would just extend the object in the first place.. Like I said before, this seems a bit stinky to begin with...

What is your reasoning behind this?
CoderGoblin wrote:
Jcart wrote:nasty code smelling goblin.
Hey I think you're cute too. :lol:
:lol:

Posted: Fri Sep 21, 2007 12:14 pm
by kendall
i guess u r right Jcart...

sigh...its depressing to have hope and see it shot to bits by reality

my reason behind this is that i built a method of the class based upon the "situation" i needed this method to handle...I note that if had to use the class again I may need to "alter" the method inorder to suit the situation as needed. This meant that the class itself would not be the same each time i use it.

So i wanted to be able to keep reusing the class un altered and just plugin a custom method each thime...

yes i cud just extended it each time but it takes away from standardization i think...i guess im looking for a more concise way of dealing with it

Posted: Fri Sep 21, 2007 12:17 pm
by John Cartwright
kendall wrote:i guess u r right Jcart...

sigh...its depressing to have hope and see it shot to bits by reality
Explain your reasoning and perhaps some clearer examples.. it's hard to suggest something without an understanding of the big picture.

Posted: Fri Sep 21, 2007 12:23 pm
by kendall
sorry didnt recognised u asked see the post edit

Posted: Fri Sep 21, 2007 12:27 pm
by John Cartwright
Having classes extend another class, especially if there is a defined interface is standardized..

In light of the edit you've made, I would definitely extend a common object.

Posted: Fri Sep 21, 2007 12:35 pm
by kendall
yes but why is it that i can...

Code: Select all

$func = 'createimagejpeg';
$func($img);
but i can't

Code: Select all

function test(){
}

$a = new class();
$a->func = 'test';
$a->func();
:?: :?:

Posted: Fri Sep 21, 2007 12:37 pm
by John Cartwright
I don't see how those two examples are related.. however I think I know what you meant by it.

Either way, I've already explained how to accomplish the latter.