View Full Version : pointer to static member function?
Malone
02-14-2005, 12:52 PM
Ok, I have class A with a static function foo.
I want to be able to call this using a pseudo function-pointer as shown below.
Such code works fine on normal functions.
But I get a "call to undefined function A::foo() ..." error when I try the code below.
class A {
function foo($param1, $param2) {
// do stuff
}
}
function bar($param1, $param2) {
// do stuff
}
...
$fptr = 'bar';
$x = 4;
$y = 5;
$fptr($x, $y); # works great!
$fptr = 'A::foo';
$fptr($x, $y); # produces error!
Justin
02-14-2005, 05:30 PM
Ok, I have class A with a static function foo.
I want to be able to call this using a pseudo function-pointer as shown below.
Such code works fine on normal functions.
But I get a "call to undefined function A::foo() ..." error when I try the code below.
class A {
function foo($param1, $param2) {
// do stuff
}
}
function bar($param1, $param2) {
// do stuff
}
...
$fptr = 'bar';
$x = 4;
$y = 5;
$fptr($x, $y); # works great!
$fptr = 'A::foo';
$fptr($x, $y); # produces error!
When you call a static function, you dont normally assign it as a var.
<?php
class test {
function test(){
}
function foo($var){
print $var;
}
}
test::foo('test');
?>
Could you clarify what you are trying to do?
Malone
02-16-2005, 12:16 PM
Yes, I realize that. Let me explain more clearly what I'm trying to do.
I have a class Customer with a function that I want to use to sort objects of type Customer.
class Customer {
var $firstname;
var $lastname;
# constructor
function Customer($first, $last) {
$this->firstname = $first;
$this->lastname = $last;
}
# compare function
function compare($c1, $c2) { # $c1 and $c2 are objects of type Customer
$result = strcmp($c1->lastname, $c2->lastname);
if ($result == 0) # same last name, compare first names
{
$result = strcmp($c1->firstname, $c2->firstname);
}
return $result;
}
}
# now some where else in my code i have an array of Customers
$customers[] = new Customer('bart','simpson');
$customers[] = new Customer('lisa','simpson');
$customers[] = new Customer('ned','flanders');
#... etc etc
# now I want to sort these Customers
$compareFunction = "Customer::compare"; # assign the compare function
usort($customers, $compareFunction); # this causes an error
# but if i define the compare function outside the class, everything works fine
# compare function
function compareCustomers($c1, $c2) { # $c1 and $c2 are objects of type Customer)
$result = strcmp($c1->lastname, $c2->lastname);
if ($result == 0) # same last name, compare first names
{
$result = strcmp($c1->firstname, $c2->firstname);
}
return $result;
}
$compareFunction = "compareCustomers"; # assign the compare function
usort($customers, $compareFunction); # works great!
So I can make it work, but I would much rather allow each class to have a static member function called "compare" that i can pass to usort(), rather than defining a compare function for each class outside of the class definition. So my question is, is that possible?
Justin
02-16-2005, 01:07 PM
Yes, I realize that. Let me explain more clearly what I'm trying to do.
I have a class Customer with a function that I want to use to sort objects of type Customer.
class Customer {
var $firstname;
var $lastname;
# constructor
function Customer($first, $last) {
$this->firstname = $first;
$this->lastname = $last;
}
# compare function
function compare($c1, $c2) { # $c1 and $c2 are objects of type Customer
$result = strcmp($c1->lastname, $c2->lastname);
if ($result == 0) # same last name, compare first names
{
$result = strcmp($c1->firstname, $c2->firstname);
}
return $result;
}
}
# now some where else in my code i have an array of Customers
$customers[] = new Customer('bart','simpson');
$customers[] = new Customer('lisa','simpson');
$customers[] = new Customer('ned','flanders');
#... etc etc
# now I want to sort these Customers
$compareFunction = "Customer::compare"; # assign the compare function
usort($customers, $compareFunction); # this causes an error
# but if i define the compare function outside the class, everything works fine
# compare function
function compareCustomers($c1, $c2) { # $c1 and $c2 are objects of type Customer)
$result = strcmp($c1->lastname, $c2->lastname);
if ($result == 0) # same last name, compare first names
{
$result = strcmp($c1->firstname, $c2->firstname);
}
return $result;
}
$compareFunction = "compareCustomers"; # assign the compare function
usort($customers, $compareFunction); # works great!
So I can make it work, but I would much rather allow each class to have a static member function called "compare" that i can pass to usort(), rather than defining a compare function for each class outside of the class definition. So my question is, is that possible?
Just use Customer::compare($1, $2)
Malone
02-22-2005, 11:22 PM
But using Customer::compare($c1, $c2) doesn't work.
For instance:
# same Customer class defintion
# here is an array of Customers
# let's say it's already filled and has 100 elements
$customers;
# now i want to sort the Customers
usort($customers, "Customer::compare");
This raises an error:
"call to undefined function Customer::compare() ..."
I only see this error when I try to do this sort of thing with static class functions.
Regular functions and non-static class functions work just fine.
Is this a limitation of PHP's object-oriented system? Or am I getting the syntax wrong?
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.