For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > callback









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author callback
Jean Pierre Daviau

2006-03-17, 6:56 pm

Hi,

Is this possible?

class theCounts {
function heCounts(){}
}

class Count1 extends theCounts{

function heCounts($a){
return "$a : Ha ha! the Count One is right!";
}
}

class Count2 extends theCounts{

function heCounts($a, $b){
return "$a, $b : Ha ha! the Count Two is right!";
}
}

$a = new theCounts();

print($a::heCounts(2). "\n");

--
Thanks for your attention.

Jean Pierre Daviau
--
Easyphp1.8
Apache1.3.24
DEVC++, borland 5.5
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
http://www.jeanpierredaviau.com


Janwillem Borleffs

2006-03-18, 7:55 am

Jean Pierre Daviau wrote:
> Is this possible?
>

[...]
> $a = new theCounts();
>
> print($a::heCounts(2). "\n");
>


print theCounts::heCounts(2);

*or*

print call_user_func_array(array('theCounts', 'heCounts'), array(2));

*or*

print $a->heCounts(2);

*or*

print call_user_func_array(array($a, 'heCounts'), array(2));


BTW, the output in all cases will be nothing, as the parent class is used
(of which, the heCounts function returns nothing) and not the extending
classes.


JW


Jean Pierre Daviau

2006-03-19, 6:56 pm

This is not really a clalback because the function does not choose among the
functions. For example:
print theCounts::heCounts(2, 4);
returns 2 instead of 2 , 6.

"Janwillem Borleffs" <jw@jwscripts.com> a écrit dans le message de news:
441bf7d6$0$26841$dbd4d001@news.euronet.nl...
> Jean Pierre Daviau wrote:
> [...]
>
> print theCounts::heCounts(2);
>
> *or*
>
> print call_user_func_array(array('theCounts', 'heCounts'), array(2));
>
> *or*
>
> print $a->heCounts(2);
>
> *or*
>
> print call_user_func_array(array($a, 'heCounts'), array(2));
>
>
> BTW, the output in all cases will be nothing, as the parent class is used
> (of which, the heCounts function returns nothing) and not the extending
> classes.
>
>
> JW
>
>



Janwillem Borleffs

2006-03-19, 6:56 pm

Jean Pierre Daviau wrote:
> This is not really a clalback because the function does not choose
> among the functions. For example:
> print theCounts::heCounts(2, 4);
> returns 2 instead of 2 , 6.
>


Then, what's your definition of a callback? $a::function() isn't one
either...


JW


Oli Filth

2006-03-19, 6:56 pm

Jean Pierre Daviau said the following on 17/03/2006 22:32:
> Is this possible?
>
> class theCounts {
> function heCounts(){}
> }
>
> class Count1 extends theCounts{
>
> function heCounts($a){
> return "$a : Ha ha! the Count One is right!";
> }
> }
>
> class Count2 extends theCounts{
>
> function heCounts($a, $b){
> return "$a, $b : Ha ha! the Count Two is right!";
> }
> }
>
> $a = new theCounts();
>
> print($a::heCounts(2). "\n");
>


Yes, it's possible, if you change the last line to:

print($a->heCounts(2) . "\n");

However, it won't do anything, as the base class definition is empty.

What does this have to do with callbacks?

--
Oli
Jean Pierre Daviau

2006-03-20, 7:55 am


> What does this have to do with callbacks?

It is based on polymorphism:

// Polymorphism in Java.
class Shape {
void draw() {}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
}
class Square extends Shape {
void draw() {
System.out.println("Square.draw()");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw()");
}
}
public class Shapes {
public static void main(String[] args) {
Shape s = new Circle();
s.draw(); // print Circle.draw()
}
}


Oli Filth

2006-03-20, 6:56 pm

Jean Pierre Daviau said the following on 20/03/2006 13:44:
> It is based on polymorphism:


"Polymorphism" isn't the same as "callbacks".

An example of a callback is when you pass a pointer/reference to a
function (or in the case of PHP, the name of a function) to another
function, which it can then use.

e.g. in PHP:

function calcRectangleArea($w, $h)
{
return ($w * $h);
}


function calcRemainder($a, $b)
{
return ($a % $b);
}


function doSomeStuff($func)
{
$x = call_user_func($func, 9, 2);
echo "Answer is " . $x . "\n";
}


doSomeStuff("calcRectangleArea"); // "Answer is 18"
doSomeStuff("calcRemainder"); // "Answer is 1"

--
Oli
Jean Pierre Daviau

2006-03-20, 6:56 pm

php 5 does not do overloading too.
<?php
class Shape {
function draw() {}
function draw(int i) {
print ("int " + i);
}
function draw(bool tf) {
print ("bool " + tf);
}
}

$s = new Shape();
$s->draw(3);
?>
Fatal error: Cannot redeclare Shape::draw() in C:\Documents and
Settings\Jean Pi
erre\Bureau\Shapes.php on line 6


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com