classes: strange behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Czubilinski

    #1

    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

  • Jerry Stuckle

    #2
    Re: classes: strange behavior

    Paul Czubilinski wrote:[color=blue]
    > 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
    >[/color]

    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.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Paul Czubilinski

      #3
      Re: classes: strange behavior

      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

      Comment

      • Jerry Stuckle

        #4
        Re: classes: strange behavior

        Paul Czubilinski wrote:[color=blue]
        > 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
        >[/color]

        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Chung Leong

          #5
          Re: classes: strange behavior

          Paul Czubilinski wrote:[color=blue]
          > 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?[/color]

          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.

          Comment

          • Paul Czubilinski

            #6
            Re: classes: strange behavior

            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

            Comment

            • Chung Leong

              #7
              Re: classes: strange behavior

              Paul Czubilinski wrote:[color=blue]
              > Hi Chung,
              > include('B.php' );
              > $a = new A();
              > class A extends B {....}[/color]

              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.

              Comment

              • Paul Czubilinski

                #8
                Re: classes: strange behavior

                Thx for clear explanation.

                Paul

                Comment

                Working...