Assigning a function to a variable...

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
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Assigning a function to a variable...

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

call_user_func() might be of interest

But seriously, explain more please as this seems like a nasty code smelling goblin.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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:
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Jcart wrote:nasty code smelling goblin.
Hey I think you're cute too. :lol:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Why not just allow the user to extend the class?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
Last edited by kendall on Fri Sep 21, 2007 12:20 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

sorry didnt recognised u asked see the post edit
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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();
:?: :?:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply