470,874 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,874 developers. It's quick & easy.

classes: strange behavior

Hello,

I have a problem with code like this (PHP 5.1.4):

fila A.php:
=======

include(B.php);
class document extends obj {
.........
}

file B.php:
=======

$GLOBALS['x']->new we();

function __autoload($class_name) {
...here is the code to load classes...
}

file C.php
=======

class we extends obj {

private $document;

function __construct() {
$this->document = new document();
}

}
The problem is that after loading A.php into browser, PHP cannot find
document class which is defined in the A.php file! The problem is even
more strange - if document class doesnt extends obj class than
everything is ok.

(The problem exact is that __autoload special function is called when
in file C.php constructor of class we try to make the document object
(all other classes like obj has been already loaded).

Do you have any idea what is wrong with that?

Paul

May 6 '06 #1
7 1404
Paul Czubilinski wrote:
Hello,

I have a problem with code like this (PHP 5.1.4):

fila A.php:
=======

include(B.php);
class document extends obj {
.........
}

file B.php:
=======

$GLOBALS['x']->new we();

function __autoload($class_name) {
...here is the code to load classes...
}

file C.php
=======

class we extends obj {

private $document;

function __construct() {
$this->document = new document();
}

}
The problem is that after loading A.php into browser, PHP cannot find
document class which is defined in the A.php file! The problem is even
more strange - if document class doesnt extends obj class than
everything is ok.

(The problem exact is that __autoload special function is called when
in file C.php constructor of class we try to make the document object
(all other classes like obj has been already loaded).

Do you have any idea what is wrong with that?

Paul


Paul,

I'm not exactly sure of what your problem is here. But I think you may be
misunderstanding how PHP works.

First of all A.php is not "loaded into the browser". The browser may load the
page - but this means the PHP is executed on the server and its output
(generally html or xml) is sent to the browser.

Once it completes execution, all the PHP stuff is discarded (other than data
saved in $_SESSION). The next page loaded will start fresh.

So in order for C.php to know anything about document, C.php must include A.php.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #2
Hi Jerry,

Sure - I made some mind shortcut writing that 'A.php' is loaded into
browser. I mean - A.php is executed on the server. The basic problem is
that class definition which is inside file A.php is not visible for the
PHP when executing code from other files (which are included from
A.php)

Paul

May 6 '06 #3
Paul Czubilinski wrote:
Hi Jerry,

Sure - I made some mind shortcut writing that 'A.php' is loaded into
browser. I mean - A.php is executed on the server. The basic problem is
that class definition which is inside file A.php is not visible for the
PHP when executing code from other files (which are included from
A.php)

Paul


OK, Paul,

Well, as long as the class definition appears before the includes for the other
files, they should be able to use it. But if the definition isn't until after
the includes, they will not be able to use it.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #4
Paul Czubilinski wrote:
Hello,

I have a problem with code like this (PHP 5.1.4):

fila A.php:
=======

include(B.php);
class document extends obj {
.........
}

file B.php:
=======

$GLOBALS['x']->new we();

function __autoload($class_name) {
...here is the code to load classes...
}

file C.php
=======

class we extends obj {

private $document;

function __construct() {
$this->document = new document();
}

}
The problem is that after loading A.php into browser, PHP cannot find
document class which is defined in the A.php file! The problem is even
more strange - if document class doesnt extends obj class than
everything is ok.

(The problem exact is that __autoload special function is called when
in file C.php constructor of class we try to make the document object
(all other classes like obj has been already loaded).

Do you have any idea what is wrong with that?


There are limits to PHP's ability to resolve class cross-dependency. I
think you might have bumped into one. I can't quite figure out the load
order of the classes in your example, but I suspect the problem has
something to do with class autoloading. When a class is defined outside
any runtime directives (such as a function), PHP tries to resolve a
class at compile time as well as runtime. Consider the following:

class A extends B {
}

class B {
}

When PHP first encounter A at compile time, there is no way for it to
resolve the class, as it hasn't seen B yet. So it quietly skips over
it, moving on to the definition of B. When PHP runs the code in global
scope, it'll try to resolve A again. This time, B is defined, so
everything is okay.

The situation changes when the definitions are inside a function:

function dingo() {

class A extends B {
}

class B {
}

}

As the function might never be called, PHP cannot resolve either class
at compile time. Both A and B have to be resolved at runtime. Because A
is declared before B, PHP will try to resolve it first--and fail.

May 7 '06 #5
Hi Chung,

I tried to simplify the problem as much as possible and it goes as
follow:

This works
========

class B { ... }
$a = new A();
class A extends B {...}

This doesn't work
==========

file A.php
-------------
include('B.php');
$a = new A();
class A extends B {....}

file B.php
------------
class B { ... }

================
I understand that in the second example A cannot be defined in the
compile mode because there is no B definition however during runtime
the first line includes B definition and I cannot find the reason it
cannot define A.

Paul

May 7 '06 #6
Paul Czubilinski wrote:
Hi Chung,
include('B.php');
$a = new A();
class A extends B {....}


This doesn't work because include() is a runtime operation. B.php
doesn't just get inserted into A.php. The behavior is somewhat like a
function call.

While compiling A.php, PHP doesn't know what B is. Therefore the
definition ("binding" to be more precise) of A has to be done at
runtime. And as new A() occurs before class A {} in the code, the
operation fails.

May 7 '06 #7
Thx for clear explanation.

Paul

May 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by mem | last post: by
9 posts views Thread by Aguilar, James | last post: by
2 posts views Thread by Dave Veeneman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.