473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($cla ss_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 1515
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($cla ss_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
misunderstandin g 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*******@attgl obal.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*******@attgl obal.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($cla ss_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1924
by: mem | last post by:
Concrete classes are ones that can be instantiated directly, as long as it doesn't have pure virtual function. However, people suggest that "Don't derive from concrete classes." Does this mean that either make it abstract base class for it to be derivable or keep them as "concrete" not to be derived at all? Then what's the purpose of...
9
6630
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all...
2
9594
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined...
7
5157
by: Dennis | last post by:
I have a class named myclass that inheirits from "baseclass". There is a property of "baseclass" that I don't want exposed in the IDE. The MSDN documentation says" "A derived type can hide an inherited member by defining a new member with the same signature. This might be done to make a previously public member private or to define new...
6
2903
by: Vijairaj R | last post by:
Hi, I have a requirement to findout all the classes that are derived from a single base class. This is how I do it currently. class Test: case = class Test1(Test):
6
2254
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When...
21
5685
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
5
1495
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create an instance of an Old-Style class without defining a __contains__ but instead define a __getitem__ method in which we raise KeyError. Next we...
15
3824
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.