#101 class inheritance problem with PHP5.0.0RC3


When I run the following script, the first time it
shows '666' (correct answert), but after a reload it
shows error:
"Notice: Undefined property: b::$data in a.php on line 8"
Possibly mmcache is making some mistake while creating
or loading the cached page when one depends on another.
The testcase consists on 2 files:
// file a.php
class A
private $data = 666;
function show()
echo $this->data; // Error line after a reload
function test()
include "b.php";
$x = new B;
$a = new A;
// file b.php
class B extends A {}

I'm running PHP5.0.0RC3 with apache 2.0.49, linux 2.6.6


  • Jonathan Stanley

    Logged In: YES

    This looks like the issue I'm having with PHP5.0.0RC3 with
    Apache+SSL 1.3.29 when using Gallery 1.4.4-CVS where the
    page will load correctly first time, but subsequent times will
    fail with albums listed as empty.

    Initially I thought it may have been an issue with serialise
    ()/unserialise(), (which I wouldn't rule out as I'm not familiar
    with either the workings of MMCache or Gallery 1.x).

  • Nobody/Anonymous

    Logged In: NO

    I get this too

  • V@no

    V@no - 2004-07-18

    Logged In: YES

    try recompille PHP without using --enable-zend-multibyte

  • tss

    tss - 2004-08-13

    Logged In: YES

    I understand that it is very frustrating to see that something
    that works without mmcache does not work with mmcache...
    It could be an indication of a bug in mmcache, I agree.

    However, as a programmer with 15+ years of experience, I do
    not understand why are you doing what you are doing in your
    example code... Okay, let me explain: you are in the middle of
    the declaration of class A (you do this in a.php) and then still
    at the middle of this declaration you are including another file
    (b.php) which in itself would be okay, however, in this b.php
    file you are declaring another class (class B) based on class
    A. You see the problem? You are creating a new class (B)
    based in class A while you are not even finished declaring
    class A!

    This is because your function "test" seems to be part of class
    A. I am surprised that your program behaves as expected by
    you even without mmcache (it is kind of a miracle)...
    Shouldn't you wait with the declaration of class B (which is
    based on class A) until class A is fully declared?

    Furthermore, I must question the location where you include
    the file b.php. Do you realize that you are declaring class B
    just within the function test (which function is part of class
    A - this is even worst!)? While at the same time you are
    declaring class A in the main program therefore class A is
    global while class B is not? Class B only exists within Class A's
    function called test, while Class B is based on class A itself.
    IMO, this is totally crazy!! Why do you want to do these
    things at the first place??? Can you please explain, because I
    don't know...

  • tss

    tss - 2004-08-13

    Logged In: YES

    For reference, check this web page:

    You can NOT break up a class definition into multiple files, or
    multiple PHP blocks. The following will not work:

    class test {
    function test() {
    print 'OK';

    IMO what you are doing does not make any sense and
    according to PHP documentation it should not work at all.

  • Rodolfo Schulz de Lima

    Logged In: YES

    First of all, this test version is an over simplification of
    a really huge system. Things that seems absurd in the example
    actually makes sense in the whole system.
    This include thing that may seem strange is a way to just
    include (and parse) the file WHEN (and IF) we call the test
    function. PHP just parse and compile the function when it is
    called, not when processing the class definition. That's
    where you got it wrong. I'm not calling a file that declares
    a child class in the middle of the parent class definition. Try to change the include of 'b.php' to some unexistent file. PHP will only choke when calling test(), not in class A
    So, I think the test is perfectly sane, although it's not a
    'common' programming practice among PHP programmers.


Log in to post a comment.