For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > Advanced OO Topic









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 Advanced OO Topic
Robert Peake

2004-05-16, 11:30 pm

Hi,

I notice a strange behavior in PHP4 and wonder if anyone can explain it.

Essentially, when I extend a class from a built-in module, I can add and
access variables but can not access new functions I create within the
extended class.

This has only happened attempting to extend classes from ming, but I suspect
it may be a universal problem (?).

That is:

<?PHP
class foo {

}

class bar extends foo {
function bar() {
print "bar";
}
}

$bar = new bar();
$bar->bar(); //PRINTS "bar"
?>

Works as expected, however, in:

<?PHP

class foo extends SWFSprite {

var $x = 10;

function bar() {
print "bar";
}

}

?>

running:

<?PHP

$foo = new foo();
print $foo->x; //WORKS; PRINTS "10"
$foo->bar(); //RETURNS "Undefined Function" FATAL ERROR

?>

Can anyone explain this behavior? Will it be fixed in PHP5? Is it related to
the Zend engine? Any workarounds?

Many thanks in advance.

Best,
Robert

--
Robert Peake | Peake Professional Consulting
Robert@PeakePro.com | http://www.peakepro.com/

Nikolai Chuvakhin

2004-05-17, 5:30 am

Robert Peake <robert@peakepro.com> wrote in message
news:<BCCD76F3.6B11%robert@peakepro.com>...
>
> I notice a strange behavior in PHP4 and wonder if anyone can explain it.
>
> Essentially, when I extend a class from a built-in module, I can add and
> access variables but can not access new functions I create within the
> extended class.
>
> This has only happened attempting to extend classes from ming,


Ming extension is still experimental. This is probably the reason
you are seeing what you are seeing...

Cheers,
NC
Chung Leong

2004-05-17, 8:30 pm

"Robert Peake" <robert@peakepro.com> wrote in message
news:BCCD76F3.6B11%robert@peakepro.com...
> Can anyone explain this behavior? Will it be fixed in PHP5? Is it related

to
> the Zend engine? Any workarounds?
>
> Many thanks in advance.
>


Internal PHP classes are "special". They behave somewhat like resource
handlers. The method invocation mechanism is different from that of regular
object, because their methods are functions written in C. I'm not too
surprised that inheritance doesn't work correctly.


Subhash

2004-05-18, 4:31 pm

Not sure why this works but maybe its a work around for you!
Try reading this - http://us4.php.net/language.oop.constructor

<?PHP
class foo extends SWFSprite {
var $x = 10;
function foo() {
}

function bar() {
print "bar";
}
}

$foo = new foo();
print $foo->x;
$foo->bar();
?>
Robert Peake

2004-05-19, 2:30 am

On 5/18/04 12:52 PM, in article
fdffa0fb.0405181152.991af1@posting.google.com, "Subhash"
<subhash_daga@yahoo.com> wrote:

> <?PHP
> class foo extends SWFSprite {
> var $x = 10;
> function foo() {
> }
>
> function bar() {
> print "bar";
> }
> }
>
> $foo = new foo();
> print $foo->x;
> $foo->bar();
> ?>


HMMM... While a blank constructor does give access to the other functions in
the class, it doesn't help my cause because an extended class constructed
with a blank constructor doesn't seem to inherit the parent methods
properly. I think a real-world example might help:

<?PHP

class SWFPhotoViewer extends SWFShape {
var $SPACING = 20;
var $MARGIN_TOP = 20;
var $MARGIN_BOTTOM = 20;
var $MARGIN_LEFT = 20;
var $MARGIN_RIGHT = 20;
var $_X = 640;
var $_Y = 480;

function SWFPhotoViewer() {
$this->SWFShape();
}

function rectangle() {
$this->setLine(2,0x00,0x00,0x00);
$this->movePenTo($this->MARGIN_LEFT,$this->MARGIN_TOP);
$this->drawLineTo($this->_X - $this->MARGIN_RIGHT,0);
$this->drawLineTo($this->_X - $this->MARGIN_RIGHT,$this->_Y -
$this->MARGIN_BOTTOM);
$this->drawLineTo($this->MARGIN_LEFT,$this->_Y - $this->MARGIN_BOTTOM);
$this->drawLineTo($this->MARGIN_LEFT,$this->MARGIN_TOP);
}
}

$pv = new SWFPhotoViewer();
$pv->rectangle();
$m = new SWFMovie();
$m->add($pv);
header('Content-type: application/x-shockwave-flash');
$m->output();

?>


In this case, I get "Fatal Error: call to undefined function "rectangle"".
If I leave the class constructor totally blank, however:

<?PHP
....
function SWFPhotoViewer() {
;
}

function rectangle()
$this->setLine(2,0x00,0x00,0x00);
....
?>

I get "Fatal error: setline(): Called object is not an SWFShape" - the class
apparently doesn't understand it is an SWFShape unless it has its
constructor called. This is further confirmed if I use the shorthand
notation without the $this variable:

<?PHP
....
function SWFPhotoViewer() {
;
}

function rectangle()
setLine(2,0x00,0x00,0x00);
....
?>

I get "Fatal Error: call to undefined function "setLine()"" - again, no
understanding that it is an SWFShape class. The same happens if I put
parent::SWFShape() in the constructor as well.

Anyone have any idea what's going on here?? I'm writing an article for IPM
on Ming and would really like to package my demo application in extended
classes, but I need to be able to create methods inside the class other than
the constructor and the parent methods. After all, doing everything inside
the constructor isn't exactly what extending classes is all about...

All help appreciated.

Best,
Robert

p.s. Will this be fixed in PHP5?

Chung Leong

2004-05-22, 12:30 am

"Robert Peake" <robert@peakepro.com> wrote in message
news:BCD03F2C.6C00%robert@peakepro.com...
> On 5/18/04 12:52 PM, in article
> fdffa0fb.0405181152.991af1@posting.google.com, "Subhash"
> <subhash_daga@yahoo.com> wrote:
>
> Anyone have any idea what's going on here?? I'm writing an article for IPM
> on Ming and would really like to package my demo application in extended
> classes, but I need to be able to create methods inside the class other

than
> the constructor and the parent methods. After all, doing everything inside
> the constructor isn't exactly what extending classes is all about...
>
> p.s. Will this be fixed in PHP5?
>


Spent some time looking at the PHP source code and realized that there isn't
anything in the Zend engine which would prevent extending an internal class.
Extending the internal class Directory works correctly:

class foo extends Directory {
function bar() {
echo "Chicken";
}
}

$cow = new foo();
$cow->bar();

So the problem seems to be the result of something in the ming module.


Robert Peake

2004-05-26, 8:31 pm

On 5/21/04 8:01 PM, in article 6MGdnWBt_tGnWzPd4p2dnA@comcast.com, "Chung
Leong" <chernyshevsky@hotmail.com> wrote:

> So the problem seems to be the result of something in the ming module.


Thanks, Chung. I have filed a bug report:

https://sourceforge.net/tracker/ind...254&group_id=18
365&atid=118365

And sent a message to the Ming developer's mailing list.

Cheers,
Robert

--
Robert Peake | Peake Professional Consulting
Robert@PeakePro.com | http://www.peakepro.com/

Sponsored Links







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

Copyright 2008 codecomments.com