470,631 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

function "attributes"

is there any way to simply add an attribute like feature to a
function/method definition?

say I create a function like

function Test()
{

//....
}

but I want to assign a probability to it in the definition itself... maybe
something like

function Test()
{
static $probability = 0.234321;
//....
}

Now is there any way that I can get the probability from the function? I'm
using function pointers to call the functions in a random way. Essentially
having a list of functions that have assigned probabilities and calling them
based on that probability(I don't want to really seperate the probability
from the definition.
Ultimately it would be nice to extend the syntax to handle a new keyword
like

function Test():[Probability=0.123421]
{

}

or something like that but I know thats not going to happen.

Thanks,
Jon
Jun 7 '07 #1
12 2177
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon Slaughter wrote:
is there any way to simply add an attribute like feature to a
function/method definition? [snip]
You want the "Strategy" pattern, making the function into an object:

class TestFunction
{
var $probability = 0.234321;
function call() { /* ... */ }
}

$func = new TestFunction();
$func->call();

PHP does not treat anonymous functions as first-class objects, unlike
JavaScript, so some acrobatics are necessary to get this working.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGZ2e0qTO+fYacSNoRAtagAJ9IV0oTUmLhpSlWSA1LBs BY7TYjCwCfft6H
noiOwWISbfNGx8RGy/+EfBM=
=er7e
-----END PGP SIGNATURE-----
Jun 7 '07 #2

"Edward Z. Yang" <ed*********@thewritingpot.comwrote in message
news:46**************@thewritingpot.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon Slaughter wrote:
>is there any way to simply add an attribute like feature to a
function/method definition? [snip]

You want the "Strategy" pattern, making the function into an object:

class TestFunction
{
var $probability = 0.234321;
function call() { /* ... */ }
}

$func = new TestFunction();
$func->call();

PHP does not treat anonymous functions as first-class objects, unlike
JavaScript, so some acrobatics are necessary to get this working.
This is more work than I'm looking for unless I can someone "wrap" a
function into a functor.
Jun 7 '07 #3
Edward Z. Yang wrote:
class TestFunction
{
var $probability = 0.234321;
function call() { /* ... */ }
}

$func = new TestFunction();
$func->call();
The other possibility is to use a static variable and function. The
example below is PHP5 code -- I have no idea if it's possible to translate
it to PHP4.

class Test
{
public static $probability = 0.234321;

public static function call()
{
$x = self::$probability * 1000000;
$r = rand(0, 1000000);
return ($r<$x);
}
}

// Using the function
$truth = Test::call();

// Retrieving the "attribute"
$prob = Test::$probability;

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 103 days, 14:54.]

URLs in demiblog
http://tobyinkster.co.uk/blog/2007/05/31/demiblog-urls/
Jun 7 '07 #4
Jon Slaughter wrote:
is there any way to simply add an attribute like feature to a
function/method definition?

say I create a function like

function Test()
{

//....
}

but I want to assign a probability to it in the definition itself... maybe
something like

function Test()
{
static $probability = 0.234321;
//....
}

Now is there any way that I can get the probability from the function? I'm
using function pointers to call the functions in a random way. Essentially
having a list of functions that have assigned probabilities and calling
them based on that probability(I don't want to really seperate the
probability from the definition.
Ultimately it would be nice to extend the syntax to handle a new keyword
like

function Test():[Probability=0.123421]
{

}

or something like that but I know thats not going to happen.

Thanks,
Jon
Hi Jon,

A simple idea but maybe it works in your situation:
1) Make your function a class.
2) Instantiate one for each function you need. (Function should now be named
method in OOP)
3) Add a simple method that stores name/value pairs you like to add to that
instance, simply storing them in an instancevariable hashed array.

class TestFunction{
var $addedValues;
function addValues($name,$value) {
$addedValues[$name]=$value;
}

function getValues(){
return $addedValues;
}
// rest goes here
}

$test1 = new TestFunction();
$test1->addValues("probability",0.234321);
Is such an approach of any use in your situation?

Regards,
Erwin Moller
Jun 7 '07 #5
Erwin Moller wrote:
Jon Slaughter wrote:
>is there any way to simply add an attribute like feature to a
function/method definition?

say I create a function like

function Test()
{

//....
}

but I want to assign a probability to it in the definition itself...
maybe something like

function Test()
{
static $probability = 0.234321;
//....
}

Now is there any way that I can get the probability from the function?
I'm using function pointers to call the functions in a random way.
Essentially having a list of functions that have assigned probabilities
and calling them based on that probability(I don't want to really
seperate the probability from the definition.
Ultimately it would be nice to extend the syntax to handle a new keyword
like

function Test():[Probability=0.123421]
{

}

or something like that but I know thats not going to happen.

Thanks,
Jon

Hi Jon,

A simple idea but maybe it works in your situation:
1) Make your function a class.
2) Instantiate one for each function you need. (Function should now be
named method in OOP)
3) Add a simple method that stores name/value pairs you like to add to
that instance, simply storing them in an instancevariable hashed array.

class TestFunction{
var $addedValues;
var $addedValues = array();

is clearer. :-)
function addValues($name,$value) {
$addedValues[$name]=$value;
}

function getValues(){
return $addedValues;
}
// rest goes here
}

$test1 = new TestFunction();
$test1->addValues("probability",0.234321);
Is such an approach of any use in your situation?

Regards,
Erwin Moller
Jun 7 '07 #6

"Erwin Moller"
<si******************************************@spam yourself.comwrote in
message news:46*********************@news.xs4all.nl...
Jon Slaughter wrote:
>is there any way to simply add an attribute like feature to a
function/method definition?

say I create a function like

function Test()
{

//....
}

but I want to assign a probability to it in the definition itself...
maybe
something like

function Test()
{
static $probability = 0.234321;
//....
}

Now is there any way that I can get the probability from the function?
I'm
using function pointers to call the functions in a random way.
Essentially
having a list of functions that have assigned probabilities and calling
them based on that probability(I don't want to really seperate the
probability from the definition.
Ultimately it would be nice to extend the syntax to handle a new keyword
like

function Test():[Probability=0.123421]
{

}

or something like that but I know thats not going to happen.

Thanks,
Jon

Hi Jon,

A simple idea but maybe it works in your situation:
1) Make your function a class.
2) Instantiate one for each function you need. (Function should now be
named
method in OOP)
3) Add a simple method that stores name/value pairs you like to add to
that
instance, simply storing them in an instancevariable hashed array.

class TestFunction{
var $addedValues;
function addValues($name,$value) {
$addedValues[$name]=$value;
}

function getValues(){
return $addedValues;
}
// rest goes here
}

$test1 = new TestFunction();
$test1->addValues("probability",0.234321);
Is such an approach of any use in your situation?

Its not that I can't use it but I feel that its to much "work". It defeats
the purpose ot trying to make it easier. Essentially I would have to wrap
all the functions into functors for just one attribute. Essentially what I
need is a way to add meta data. I think Toby's method might work.

Thanks,
Jon
Jun 7 '07 #7

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:uu************@ophelia.g5n.co.uk...
Edward Z. Yang wrote:
>class TestFunction
{
var $probability = 0.234321;
function call() { /* ... */ }
}

$func = new TestFunction();
$func->call();

The other possibility is to use a static variable and function. The
example below is PHP5 code -- I have no idea if it's possible to translate
it to PHP4.
class Test
{
public static $probability = 0.234321;

public static function call()
{
$x = self::$probability * 1000000;
$r = rand(0, 1000000);
return ($r<$x);
}
}

// Using the function
$truth = Test::call();

// Retrieving the "attribute"
$prob = Test::$probability;

So I have to call the function first? ;/ Isn't going to work ;/ I have to
get the probabilities from the function to determine which function to call
;/
Ultimately it seems I'm going to have to use functors or just write a method
to add the functions that takes a pointer but just seems a little overkill
just to add one "attribute".

thanks,
Jon

Jun 7 '07 #8
Jon,

What are you trying to accomplish? Do you have a list of functions,
and you want to call each one if the probability of it being called is
above a certain threshold? If so, a very simple approach might be to
assign the probabilities and functions to an array.

See if this is what you're trying to do, or perhaps clarify it to me/
us:

$funcary[] = array('probability'=>.75, 'funcname'=>'function1');
$funcary[] = array('probability'=>.47, 'funcname'=>'function2');
$funcary[] = array('probability'=>.99, 'funcname'=>'function3');

$curprob = rand(0,100)/100;

foreach($funcary as $afunc=>$ary){
if ($afunc['probability'] < $curprob) $afunc['funcname']();
}

Where 'function1' etc. are the names of your functions. I didn't test
this at all, fyi, but I think it should work.

Aerik

Jun 7 '07 #9

"Aerik" <as*****@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Jon,

What are you trying to accomplish? Do you have a list of functions,
and you want to call each one if the probability of it being called is
above a certain threshold? If so, a very simple approach might be to
assign the probabilities and functions to an array.

See if this is what you're trying to do, or perhaps clarify it to me/
us:

$funcary[] = array('probability'=>.75, 'funcname'=>'function1');
$funcary[] = array('probability'=>.47, 'funcname'=>'function2');
$funcary[] = array('probability'=>.99, 'funcname'=>'function3');

$curprob = rand(0,100)/100;

foreach($funcary as $afunc=>$ary){
if ($afunc['probability'] < $curprob) $afunc['funcname']();
}

Where 'function1' etc. are the names of your functions. I didn't test
this at all, fyi, but I think it should work.

Aerik
No, that is my application but what I'm trying to do is in some way assign
the probability to the function that is inline with the function definition.
If, say, I have to create 100 of these functions then then I want to
minimize the amount of excess code just to add the probability part.

I can automatically include the functions because they are in a class and I
use get_class_methods to get them... but this just gives me the functions
and in not the probabilities.

For your method you have to know the function name and you have to add them
to the array manually. With my method I do it automatically but I cannot
easily handle the probabilities. (right now I just use 1 as default so I can
get on with the other parts.

If there was some other function such as get_function_attributes for
example, or get_function_vars which returns the variables used in a
function(or static vars) then I could extract the probabilities
automatically. Of course there isn't anything like this.

I suppose I could open up the file as a text file and parse it myself but
this is more work that I'd rather not do unless someone as an easy way and
it also seems kinda inefficient.

Jon
Jun 7 '07 #10
Jon Slaughter wrote:
So I have to call the function first?
No. Equally, this would work:

if (Test::$probability < 0.5)
{
$truth = Test::call();
}

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 104 days, 1:37.]

URLs in demiblog
http://tobyinkster.co.uk/blog/2007/05/31/demiblog-urls/
Jun 7 '07 #11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon Slaughter wrote:
This is more work than I'm looking for unless I can someone "wrap" a
function into a functor.
It is, however, the cleanest way of implementing it in PHP. Otherwise,
you'll have to use static data or globals.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGaJuGqTO+fYacSNoRAvubAJsG9R1H20WKtxleC7BsIT CaeHU2NwCaA3DW
QNiJdnPN5t070dSeS0DDzvk=
=h6ds
-----END PGP SIGNATURE-----
Jun 8 '07 #12
Jon Slaughter wrote:
is there any way to simply add an attribute like feature to a
function/method definition?

say I create a function like

function Test()
{

//....
}

but I want to assign a probability to it in the definition itself... maybe
something like

function Test()
{
static $probability = 0.234321;
//....
}

Now is there any way that I can get the probability from the function? I'm
using function pointers to call the functions in a random way. Essentially
having a list of functions that have assigned probabilities and calling them
based on that probability(I don't want to really seperate the probability
from the definition.
Ultimately it would be nice to extend the syntax to handle a new keyword
like

function Test():[Probability=0.123421]
{

}

or something like that but I know thats not going to happen.

Thanks,
Jon

Ok, this is ugly but if you name your functions by their probability:

function _0_234321()
{ // test function
$probability = 0.234321;
echo "$probability<br>";
...
}
$probability = 0.234321;
$func_to_call = '_'.str_replace('.','_',$probability);
//echo $func_to_call.'<br>';
call_user_func($func_to_call);

.... or am I missing the point?

Norm
Jun 8 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

68 posts views Thread by Marco Bubke | last post: by
15 posts views Thread by Jordan Rastrick | last post: by
2 posts views Thread by prabhupr | last post: by
5 posts views Thread by akonsu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.