|
Posted by Erwin Moller on 06/07/07 08:57
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
[Back to original message]
|