-> or :: ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a@b.c

    #1

    -> or :: ?

    hi all,

    I'm a bit confused with this and I hope you can help me:

    I have a "Mother" class and a "child" that extends Mother.
    The child's constructor should call Mother's constructor.

    Now the question is:
    How should child's constructor call mother's constructor?
    a) Mother::Mother( );
    b) $this->Mother();

    I guess the second way is the right one, but I'm confused because:
    1) both methods work;
    2) both methods are in the manual;

    can you help me to understand this (so I can sleep again ;-) )?

    TIA
    FR


    ----
    The code:

    <?php

    class Mother
    {
    var $v;
    function Mother()
    {
    echo "I'm the Mother<br>\n";
    $this->v=1;
    }
    }

    class ChildA extends Mother
    {
    function ChildA()
    {
    Mother::Mother( ); // <--- is this correct? why?
    echo "I'm the childA<br>\n";
    }
    }

    class ChildB extends Mother
    {
    function ChildB()
    {
    $this->Mother(); // <--- is this correct? why?
    echo "I'm the childB<br>\n";
    }
    }

    $a=new childA();
    $b=new childB();

    echo "\$a->v: $a->v<br>\n";
    echo "\$b->v: $b->v<br>\n";

    ?>
  • Berislav Lopac

    #2
    Re: -&gt; or :: ?

    a@b.c wrote:[color=blue]
    > hi all,
    >
    > I'm a bit confused with this and I hope you can help me:
    >
    > I have a "Mother" class and a "child" that extends Mother.
    > The child's constructor should call Mother's constructor.
    >
    > Now the question is:
    > How should child's constructor call mother's constructor?
    > a) Mother::Mother( );
    > b) $this->Mother();
    >
    > I guess the second way is the right one, but I'm confused because:
    > 1) both methods work;
    > 2) both methods are in the manual;
    >
    > can you help me to understand this (so I can sleep again ;-) )?[/color]

    You should understand OOP a bit more.

    Using :: is invoking a class's method as static, i.e. directly from the
    class; essentially, there is no difference between that and classic
    user-defined functions, except that you can have several functions of the
    same name and just call them using a different "prefix" (class name) in
    different situations.

    The -> notation is used when you call a method, ie. a function attached to
    an actual object.

    In code, you use static method like this:

    YourClass::your Function();

    However, to call a method you need to have an object instantiated first:

    $object = new YourClass();
    $object->yourFunction() ;

    The advantage of this is that the method is "attached" to the object, and
    already "knows" everything about it. You can do the same by a static method
    (or a classic function), but you would have to pass the object as a
    parameter first.

    This method thing might not seem like much, but this is just a facet of
    OOP -- there is inheritance, with subclasses extending other classes, and
    polymorphism, with same methods doing different things depending on their
    object's class (so accelerate() does one for your generic Car class, and
    something completely different for its Ferrari subclass) and so on.

    Berislav

    --
    If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
    Groucho, Chico, and Harpo, then Usenet is Zeppo.


    Comment

    • Zurab Davitiani

      #3
      Re: -&gt; or :: ?

      Berislav Lopac wrote:
      [color=blue]
      > You should understand OOP a bit more.
      >
      > Using :: is invoking a class's method as static, i.e. directly from the
      > class; essentially, there is no difference between that and classic
      > user-defined functions, except that you can have several functions of the
      > same name and just call them using a different "prefix" (class name) in
      > different situations.
      >
      > The -> notation is used when you call a method, ie. a function attached to
      > an actual object.
      >
      > In code, you use static method like this:
      >
      > YourClass::your Function();
      >
      > However, to call a method you need to have an object instantiated first:
      >
      > $object = new YourClass();
      > $object->yourFunction() ;[/color]

      Yeah, but calling the parent class constructor from a child's constructor
      either way accomplishes the same - i.e. not a static function call, but a
      method call that may change the values of the properties of the object
      being instantiated. That's how I understood the OP's question. There is one
      more way to get the same result in PHP4:

      parent::ParentC lass();

      and, in fact, in PHP5, the "correct" way to call parent class constructor
      is:

      parent::__const ruct();

      Comment

      • Chung Leong

        #4
        Re: -&gt; or :: ?


        <a@b.c> wrote in message news:40f3b1bd$0 $16454$626a14ce @news.free.fr.. .[color=blue]
        > hi all,
        >
        > I'm a bit confused with this and I hope you can help me:
        >
        > I have a "Mother" class and a "child" that extends Mother.
        > The child's constructor should call Mother's constructor.
        >
        > Now the question is:
        > How should child's constructor call mother's constructor?
        > a) Mother::Mother( );
        > b) $this->Mother();
        >
        > I guess the second way is the right one, but I'm confused because:
        > 1) both methods work;
        > 2) both methods are in the manual;[/color]

        I would say that invocation through :: is the preferred syntax. Basically a
        way to acknowledge the difference behind inherited the default behavior and
        calling a method. As you noted, the two works the same. The function invoke
        statically would still get the $this variable.


        Comment

        Working...