Parent Objects

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

    #1

    Parent Objects

    I have

    class thechild{
    function sayTest()
    {
    parent::test(); // this right???
    }
    }

    class theparent{
    function test()
    {
    echo "test";
    }
    function __construct()
    {
    $e = new thechild();
    $e->sayTest();
    }
    }

    How do i get this to work ? I am trying to get a one class (A) to
    create another class (B) within it, but the class B must be able to
    call a function from class A. Any ideas?

    Thanks
    Paul Jenkins


  • Jerry Stuckle

    #2
    Re: Parent Objects

    paul.j wrote:
    I have
    >
    class thechild{
    function sayTest()
    {
    parent::test(); // this right???
    }
    }
    >
    class theparent{
    function test()
    {
    echo "test";
    }
    function __construct()
    {
    $e = new thechild();
    $e->sayTest();
    }
    }
    >
    How do i get this to work ? I am trying to get a one class (A) to
    create another class (B) within it, but the class B must be able to
    call a function from class A. Any ideas?
    >
    Thanks
    Paul Jenkins

    >
    No, this is not correct. You have no parent-child relationship between
    them; theparent's constructor just creates a thechild object (then
    throws it away when the constructor exits.

    And think about this - what happens if you just had:

    function myfunc() {
    $c = new thechild();
    ...
    }

    IOW, you're creating a new thechild object outside of a theparent
    object. What would the statement "parent::text() " do then?

    Look at the "extends" keyword. Use it to derive thechild from theparent.


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Sanders Kaufman

      #3
      Re: Parent Objects

      Jerry Stuckle wrote:
      IOW, you're creating a new thechild object outside of a theparent
      object. What would the statement "parent::text() " do then?

      What does "::" mean?
      Is that a shortcut for "->"?

      Comment

      • Heiko Richler

        #4
        Re: Parent Objects

        Sanders Kaufman wrote:
        What does "::" mean?
        Is that a shortcut for "->"?
        No, it is the Scope Resolution Operator


        Heiko
        --
        http://portal.richler.de/ Namensportal zu Richler
        http://www.richler.de/ Heiko Richler: Computer - Know How!
        http://www.richler.info/ private Homepage

        Comment

        • Sanders Kaufman

          #5
          Re: Parent Objects

          Heiko Richler wrote:
          Sanders Kaufman wrote:
          >What does "::" mean?
          >Is that a shortcut for "->"?
          >
          No, it is the Scope Resolution Operator

          >
          Heiko
          I read that, but I'm still a little confused.
          When I use a class's methods and such, I've been using "->".
          But it seems like this "::" is used the same way.
          What's the difference?

          Comment

          • Rik

            #6
            Re: Parent Objects

            Sanders Kaufman wrote:
            Heiko Richler wrote:
            >Sanders Kaufman wrote:
            >>What does "::" mean?
            >>Is that a shortcut for "->"?
            >>
            >No, it is the Scope Resolution Operator
            >http://www.php.net/manual/en/languag...ekudotayim.php
            >>
            >Heiko
            >
            I read that, but I'm still a little confused.
            When I use a class's methods and such, I've been using "->".
            But it seems like this "::" is used the same way.
            What's the difference?
            Simplified: it's the difference between objects and classes.
            --
            Rik Wasmus


            Comment

            • Rik

              #7
              Re: Parent Objects

              paul.j wrote:
              I have
              >
              class thechild{
              function sayTest()
              {
              parent::test(); // this right???
              }
              }
              >
              class theparent{
              function test()
              {
              echo "test";
              }
              function __construct()
              {
              $e = new thechild();
              $e->sayTest();
              }
              }
              >
              How do i get this to work ? I am trying to get a one class (A) to
              create another class (B) within it, but the class B must be able to
              call a function from class A. Any ideas?
              You could make named class variables. It's up to yourself they work though:

              class thechild{
              var $parent;
              function __construct(&$o bject){
              $this->parent =& $object;
              }
              function sayTest()
              {
              $this->parent->test();
              }
              }
              class theparent{
              var $child;
              function test()
              {
              echo "test";
              }
              function __construct()
              {
              $this->child = new thechild($this) ;
              $this->child->sayTest();
              }
              }
              --
              Rik Wasmus


              Comment

              • Sanders Kaufman

                #8
                Re: Parent Objects

                Rik wrote:
                Sanders Kaufman wrote:
                >>>What does "::" mean?
                >>>Is that a shortcut for "->"?
                >>No, it is the Scope Resolution Operator
                >>http://www.php.net/manual/en/languag...ekudotayim.php
                >I read that, but I'm still a little confused.
                >When I use a class's methods and such, I've been using "->".
                >But it seems like this "::" is used the same way.
                >What's the difference?
                >
                Simplified: it's the difference between objects and classes.
                Two part question here:

                [1]
                OK - but on a more practical level, what's the difference.
                I mean -like, in my code I'll create a class.
                In that class, I'll reference it's members with "$this->member".
                Then, somewhere else I'll create an object based on the class
                and reference it's members with "$class->member".

                Where would I use "::"?

                [2]
                Perhaps I would understand better if I understood what is meant
                by the phrase "scope resolution operator".
                So why is it called that?
                Maybe more specifically, what does "resolution " mean in this
                context?


                Comment

                • Rik

                  #9
                  Re: Parent Objects

                  Sanders Kaufman wrote:
                  Rik wrote:
                  >Sanders Kaufman wrote:
                  >
                  >>>>What does "::" mean?
                  >>>>Is that a shortcut for "->"?
                  >
                  >>>No, it is the Scope Resolution Operator
                  >>>http://www.php.net/manual/en/languag...ekudotayim.php
                  >
                  >>I read that, but I'm still a little confused.
                  >>When I use a class's methods and such, I've been using "->".
                  >>But it seems like this "::" is used the same way.
                  >>What's the difference?
                  >>
                  >Simplified: it's the difference between objects and classes.
                  >
                  Two part question here:
                  >
                  [1]
                  OK - but on a more practical level, what's the difference.
                  I mean -like, in my code I'll create a class.
                  In that class, I'll reference it's members with "$this->member".
                  Then, somewhere else I'll create an object based on the class
                  and reference it's members with "$class->member".
                  >
                  Where would I use "::"?
                  You wouldn't, as it are all objects, and not classes. You'd only need it if
                  your class extended another class and you wished to call a parents method.


                  --
                  Rik Wasmus


                  Comment

                  • Michael Fesser

                    #10
                    Re: Parent Objects

                    ..oO(Sanders Kaufman)
                    >I read that, but I'm still a little confused.
                    >When I use a class's methods and such, I've been using "->".
                    >But it seems like this "::" is used the same way.
                    >What's the difference?
                    $someObject->method()
                    someClass::stat icMethod()

                    '->' requires an instance of a class, whereas '::' is used to call
                    static class methods or constants without having an object. Additionally
                    it's used to call inherited methods.

                    Micha

                    Comment

                    • Toby Inkster

                      #11
                      Re: Parent Objects

                      Sanders Kaufman wrote:
                      When I use a class's methods and such, I've been using "->".
                      But it seems like this "::" is used the same way.
                      Using '::' you can call a classes methods, constants and static functions
                      (here's the key part) without instantiating an object of that class.

                      Example:

                      <?php
                      include "Widget.class.p hp";
                      echo Widget::FOO;
                      ?>

                      Versus:

                      <?php
                      include "Widget.class.p hp";
                      $my_widget = new Widget();
                      echo $my_widget->FOO;
                      ?>

                      Another example:

                      <?php
                      class Widget
                      {
                      function exist ()
                      {
                      return isset($this);
                      }
                      }
                      $my_widget = new Widget();
                      $my_widget->exist(); // true
                      Widget::exist() ; // false
                      ?>

                      That is, '->' forces a function to run on a specific object, whereas '::'
                      runs the function without the object instance (i.e. '$this' is undefined).

                      --
                      Toby A Inkster BSc (Hons) ARCS
                      Contact Me ~ http://tobyinkster.co.uk/contact

                      Comment

                      • Sanders Kaufman

                        #12
                        Re: Parent Objects

                        Rik wrote:
                        Sanders Kaufman wrote:
                        >Where would I use "::"?
                        >
                        You wouldn't, as it are all objects, and not classes. You'd only need it if
                        your class extended another class and you wished to call a parents method.
                        >
                        http://www.php.net/manual/en/languag...ekudotayim.php
                        OH! So if my class has a method, and then another class extends
                        the class and overrides(?) the method, I can call the original
                        method with "::"?

                        Comment

                        • Sanders Kaufman

                          #13
                          Re: Parent Objects

                          Michael Fesser wrote:
                          .oO(Sanders Kaufman)
                          >
                          >I read that, but I'm still a little confused.
                          >When I use a class's methods and such, I've been using "->".
                          >But it seems like this "::" is used the same way.
                          >What's the difference?
                          >
                          $someObject->method()
                          someClass::stat icMethod()
                          >
                          '->' requires an instance of a class, whereas '::' is used to call
                          static class methods or constants without having an object. Additionally
                          it's used to call inherited methods.
                          D'oh!
                          I thought I had it - but then you said something about static stuff.
                          And I think I'm futzed up about Object v. Class here.

                          So let's see if I've got it better this way:

                          I call my OBJECT methods with "->".
                          I call my CLASS methods with "::".

                          Ja?

                          Comment

                          • Sanders Kaufman

                            #14
                            Re: Parent Objects

                            Toby Inkster wrote:
                            <?php
                            include "Widget.class.p hp";
                            echo Widget::FOO;
                            ?>
                            >
                            Versus:
                            >
                            <?php
                            include "Widget.class.p hp";
                            $my_widget = new Widget();
                            echo $my_widget->FOO;
                            ?>

                            It means that I can call the CLASS method, without
                            instantiating(? ) an object, right?

                            Man, oh man, that could be real handy for my classes that don't
                            do too much stuff in the constructor!

                            Wait... I guess I shouldn't assume here. So I'll ask - when I
                            call a class method with "::", it won't fire up the constructor,
                            will it?

                            Comment

                            • Michael Fesser

                              #15
                              Re: Parent Objects

                              ..oO(Sanders Kaufman)
                              >D'oh!
                              >I thought I had it - but then you said something about static stuff.
                              A static class method can be called without having to create an object
                              first.
                              >And I think I'm futzed up about Object v. Class here.
                              An object is an incarnation of a class, which itself is more or less
                              just a "blueprint" for creating objects.
                              >So let's see if I've got it better this way:
                              >
                              >I call my OBJECT methods with "->".
                              >I call my CLASS methods with "::".
                              Yep, that's one of its uses.

                              Micha

                              Comment

                              Working...