Professional Web Applications Themes

Advanced OO Topic - PHP Development

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 ...

  1. #1

    Default Advanced OO Topic

    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
    [email]RobertPeakePro.com[/email] | [url]http://www.peakepro.com/[/url]

    Robert Peake Guest

  2. #2

    Default Re: Advanced OO Topic

    Robert Peake <robertpeakepro.com> wrote in message
    news:<BCCD76F3.6B11%robertpeakepro.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
    Nikolai Chuvakhin Guest

  3. #3

    Default Re: Advanced OO Topic

    "Robert Peake" <robertpeakepro.com> wrote in message
    news:BCCD76F3.6B11%robertpeakepro.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.


    Chung Leong Guest

  4. #4

    Default Re: Advanced OO Topic

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

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

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

    $foo = new foo();
    print $foo->x;
    $foo->bar();
    ?>
    Subhash Guest

  5. #5

    Default Re: Advanced OO Topic

    On 5/18/04 12:52 PM, in article
    [email]fdffa0fb.0405181152.991af1posting.google.com[/email], "Subhash"
    <subhash_daga> 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?

    Robert Peake Guest

  6. #6

    Default Re: Advanced OO Topic

    "Robert Peake" <robertpeakepro.com> wrote in message
    news:BCD03F2C.6C00%robertpeakepro.com...
    > On 5/18/04 12:52 PM, in article
    > [email]fdffa0fb.0405181152.991af1posting.google.com[/email], "Subhash"
    > <subhash_daga> 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.


    Chung Leong Guest

  7. #7

    Default Re: Advanced OO Topic

    On 5/21/04 8:01 PM, in article [email]6MGdnWBt_tGnWzPd4p2dnAcomcast.com[/email], "Chung
    Leong" <chernyshevskyhotmail.com> wrote:
    > So the problem seems to be the result of something in the ming module.
    Thanks, Chung. I have filed a bug report:

    [url]https://sourceforge.net/tracker/index.php?func=detail&aid=961254&group_id=18[/url]
    365&atid=118365

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

    Cheers,
    Robert

    --
    Robert Peake | Peake Professional Consulting
    [email]RobertPeakePro.com[/email] | [url]http://www.peakepro.com/[/url]

    Robert Peake Guest

Similar Threads

  1. Off Topic but nowhere else to go
    By Simon Gare in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: November 1st, 12:24 PM
  2. I found this in a topic and thought it should be a new topic
    By Dee Holmes in forum Adobe Illustrator Macintosh
    Replies: 0
    Last Post: May 7th, 01:18 AM
  3. here's a topic
    By edmcl webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 15th, 11:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139