class inheritance problem with PHP5.0.0RC3
Brought to you by:
dstogov
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:
<?php
// 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;
$x->show();
}
}
$a = new A;
$a->test();
?>
<?
// file b.php
class B extends A {}
>?
I'm running PHP5.0.0RC3 with apache 2.0.49, linux 2.6.6
Logged In: YES
user_id=371553
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).
Logged In: NO
I get this too
Logged In: YES
user_id=550230
try recompille PHP without using --enable-zend-multibyte
Logged In: YES
user_id=1047035
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...
Logged In: YES
user_id=1047035
For reference, check this web page:
http://www.php.net/language.oop
You can NOT break up a class definition into multiple files, or
multiple PHP blocks. The following will not work:
<?php
class test {
?>
<?php
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.
Logged In: YES
user_id=513966
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
construction.
So, I think the test is perfectly sane, although it's not a
'common' programming practice among PHP programmers.