473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2342
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
3
by: Victor | last post by:
Hi, I have some sample XML and an XSD below I have written. The XSD almost does what I want. What I need is some way of enforcing that AT LEAST TWO of the attributes "TestAttribute" are "X". ...
2
by: Netto | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** When I try to use a created attribute for the tag "input", it works fine with IE 6, but it seems not to be recognized by Netscape...
4
by: Dotcom | last post by:
I have an ASP.NET application that is mysteriously acquiring height and width attributes on a particular IMG element on multiple pages. This is not being caused by someone editing or uploading new...
2
by: Daren Hawes | last post by:
Hi I need to add an attribute to a Textbox to make it read only. To add a CSS I use DeptDate.Attributes("Class") = "textInput" That adds 'Class="textinput"', but the readonly is like..
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
0
by: whosyodaddy1019 | last post by:
Does anyone have any code that can do this. From what I understand, these are flags in the "userAccountControl" properties but unsure how to get it unchecked. Can anyone help? Imports System...
5
by: akonsu | last post by:
hello, i need to add properties to instances dynamically during run time. this is because their names are determined by the database contents. so far i found a way to add methods on demand: ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.