class inside another class - PHP 4

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

    class inside another class - PHP 4

    Hello.

    I've got a strange situation.
    I've got class Handlers it's only:

    class Handlers
    {
    var $DB;
    var $XML;
    }

    In my PHP script I want to do sth like this:

    $Main = new Handlers();
    $Main->DB = new DataBase();
    $Main->XML = new XML();

    but when inside eg. CreateNode method in XML class I want to run query
    using: $this->DB->RunQuery();
    it's of course fatal error...

    I saw in one script that such classes
    can be created but i don't remember where I found it...

    Can it be writen in PHP 4?

    thanks in advance

    --
    best regards
    R
  • Matthias Esken

    #2
    Re: class inside another class - PHP 4

    R wrote:
    [color=blue]
    > using: $this->DB->RunQuery();
    >
    > [...]
    >
    > Can it be writen in PHP 4?[/color]

    This works with PHP5. The solution for PHP4 is:

    $db = &$this->DB;
    $db->RunQuery();

    Regards,
    Matthias

    Comment

    • Zurab Davitiani

      #3
      Re: class inside another class - PHP 4

      Matthias Esken wrote:
      [color=blue]
      > R wrote:
      >[color=green]
      >> using: $this->DB->RunQuery();
      >>
      >> [...]
      >>
      >> Can it be writen in PHP 4?[/color]
      >
      > This works with PHP5. The solution for PHP4 is:
      >
      > $db = &$this->DB;
      > $db->RunQuery();[/color]

      There's nothing wrong with
      $this->DB->RunQuery();
      syntax in PHP4 as long as DB is a property and not a method.

      Comment

      • Michael Fesser

        #4
        Re: class inside another class - PHP 4

        .oO(Matthias Esken)
        [color=blue]
        >R wrote:
        >[color=green]
        >> using: $this->DB->RunQuery();
        >>
        >> [...]
        >>
        >> Can it be writen in PHP 4?[/color]
        >
        >This works with PHP5. The solution for PHP4 is:
        >
        > $db = &$this->DB;
        > $db->RunQuery();[/color]

        The above also works in PHP4. DB is a property, not a method.

        The OP's problem is that $this always points to the object that calls
        the method, inside the XML-methods it points to $XML, not to the parent
        object. The XML-object can't directly access the methods of the DB-
        object because it simply doesn't know about it.

        One possible solution would be to implement a class method that returns
        the required object:

        PHP4:

        $db = &Handlers::getI nstance('DB');
        $db->RunQuery();

        PHP5:

        Handlers::getIn stance('DB')->RunQuery();

        HTH
        Micha

        Comment

        • R

          #5
          Re: class inside another class - PHP 4

          Zurab Davitiani wrote:
          [color=blue]
          > There's nothing wrong with
          > $this->DB->RunQuery();
          > syntax in PHP4 as long as DB is a property and not a method.[/color]

          OK.

          But I'm sure, that I saw in one PHP 4 script that such methods where used.
          I used them by myself $this->Class1->ItsMethod() syntax inside eg.
          $this->Class2->ItsMethod()

          I used such object some time ago but I forgot the URL... I tried to
          google but I failed

          thanks in advance for Your help

          --
          best regards
          R

          Comment

          Working...