oop question

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

    oop question



    Why does it print out below like it says it does? Why doesn't it
    print out just foo's methods?

    Thanks.

    class Bar
    {
    public function test() {
    $this->testPrivate( );
    $this->testPublic() ;
    }

    public function testPublic() {
    echo "Bar::testPubli c\n";
    }

    private function testPrivate() {
    echo "Bar::testPriva te\n";
    }
    }

    class Foo extends Bar
    {
    public function testPublic() {
    echo "Foo::testPubli c\n";
    }

    private function testPrivate() {
    echo "Foo::testPriva te\n";
    }
    }

    $myFoo = new foo();
    $myFoo->test(); // Bar::testPrivat e
    // Foo::testPublic
  • jmDesktop

    #2
    Re: oop question

    On Sep 16, 12:25 pm, jmDesktop <needin4mat...@ gmail.comwrote:

    >
    Why does it print out below like it says it does?    Why doesn't it
    print out just foo's methods?
    >
    Thanks.
    >
    class Bar
    {
        public function test() {
            $this->testPrivate( );
            $this->testPublic() ;
        }
    >
        public function testPublic() {
            echo "Bar::testPubli c\n";
        }
    >
        private function testPrivate() {
            echo "Bar::testPriva te\n";
        }
    >
    }
    >
    class Foo extends Bar
    {
        public function testPublic() {
            echo "Foo::testPubli c\n";
        }
    >
        private function testPrivate() {
            echo "Foo::testPriva te\n";
        }
    >
    }
    >
    $myFoo = new foo();
    $myFoo->test(); // Bar::testPrivat e
                    // Foo::testPublic
    because foo's is private and bar can't see it. but when I changed
    foo's to public testPrivate, it still showed bar's. I had to make the
    both public to see foo's.

    Comment

    • Jerry Stuckle

      #3
      Re: oop question

      jmDesktop wrote:
      On Sep 16, 12:25 pm, jmDesktop <needin4mat...@ gmail.comwrote:
      >http://php.net/manual/en/language.oop5.visibility.php
      >>
      >Why does it print out below like it says it does? Why doesn't it
      >print out just foo's methods?
      >>
      >Thanks.
      >>
      >class Bar
      >{
      > public function test() {
      > $this->testPrivate( );
      > $this->testPublic() ;
      > }
      >>
      > public function testPublic() {
      > echo "Bar::testPubli c\n";
      > }
      >>
      > private function testPrivate() {
      > echo "Bar::testPriva te\n";
      > }
      >>
      >}
      >>
      >class Foo extends Bar
      >{
      > public function testPublic() {
      > echo "Foo::testPubli c\n";
      > }
      >>
      > private function testPrivate() {
      > echo "Foo::testPriva te\n";
      > }
      >>
      >}
      >>
      >$myFoo = new foo();
      >$myFoo->test(); // Bar::testPrivat e
      > // Foo::testPublic
      >
      because foo's is private and bar can't see it. but when I changed
      foo's to public testPrivate, it still showed bar's. I had to make the
      both public to see foo's.
      >
      That is correct operation. Even though Foo's method is public, Bar's is
      private. So Bar::test() only looks at the private function.

      To overload a function in a derived class, both base and derived class
      functions must be public.

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

      Comment

      • jmDesktop

        #4
        Re: oop question

        On Sep 16, 12:54 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        jmDesktop wrote:
        On Sep 16, 12:25 pm, jmDesktop <needin4mat...@ gmail.comwrote:
        >
        Why does it print out below like it says it does?    Why doesn't it
        print out just foo's methods?
        >
        Thanks.
        >
        class Bar
        {
            public function test() {
                $this->testPrivate( );
                $this->testPublic() ;
            }
        >
            public function testPublic() {
                echo "Bar::testPubli c\n";
            }
        >
            private function testPrivate() {
                echo "Bar::testPriva te\n";
            }
        >
        }
        >
        class Foo extends Bar
        {
            public function testPublic() {
                echo "Foo::testPubli c\n";
            }
        >
            private function testPrivate() {
                echo "Foo::testPriva te\n";
            }
        >
        }
        >
        $myFoo = new foo();
        $myFoo->test(); // Bar::testPrivat e
                        // Foo::testPublic
        >
        because foo's is private and bar can't see it.  but when I changed
        foo's to public testPrivate, it still showed bar's.  I had to make the
        both public to see foo's.
        >
        That is correct operation.  Even though Foo's method is public, Bar's is
        private.  So Bar::test() only looks at the private function.
        >
        To overload a function in a derived class, both base and derived class
        functions must be public.
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===- Hide quoted text -
        >
        - Show quoted text -
        Is that just the way that PHP handles overriding (it isn't really
        overloading is it)? I don't remember that way being anything specific
        for oop. I know some have the word virtual and overrid like C#. Or
        the derived just overrides by default. I think Java does it that way.

        Comment

        • Michael Fesser

          #5
          Re: oop question

          ..oO(Jerry Stuckle)
          >That is correct operation. Even though Foo's method is public, Bar's is
          >private. So Bar::test() only looks at the private function.
          >
          >To overload a function in a derived class, both base and derived class
          >functions must be public.
          Or protected.

          Micha

          Comment

          • Jerry Stuckle

            #6
            Re: oop question

            jmDesktop wrote:
            On Sep 16, 12:54 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            >jmDesktop wrote:
            >>On Sep 16, 12:25 pm, jmDesktop <needin4mat...@ gmail.comwrote:
            >>>http://php.net/manual/en/language.oop5.visibility.php
            >>>Why does it print out below like it says it does? Why doesn't it
            >>>print out just foo's methods?
            >>>Thanks.
            >>>class Bar
            >>>{
            >>> public function test() {
            >>> $this->testPrivate( );
            >>> $this->testPublic() ;
            >>> }
            >>> public function testPublic() {
            >>> echo "Bar::testPubli c\n";
            >>> }
            >>> private function testPrivate() {
            >>> echo "Bar::testPriva te\n";
            >>> }
            >>>}
            >>>class Foo extends Bar
            >>>{
            >>> public function testPublic() {
            >>> echo "Foo::testPubli c\n";
            >>> }
            >>> private function testPrivate() {
            >>> echo "Foo::testPriva te\n";
            >>> }
            >>>}
            >>>$myFoo = new foo();
            >>>$myFoo->test(); // Bar::testPrivat e
            >>> // Foo::testPublic
            >>because foo's is private and bar can't see it. but when I changed
            >>foo's to public testPrivate, it still showed bar's. I had to make the
            >>both public to see foo's.
            >That is correct operation. Even though Foo's method is public, Bar's is
            >private. So Bar::test() only looks at the private function.
            >>
            >To overload a function in a derived class, both base and derived class
            >functions must be public.
            >>
            >--
            >============== ====
            >Remove the "x" from my email address
            >Jerry Stuckle
            >JDS Computer Training Corp.
            >jstuck...@attg lobal.net
            >============== ====- Hide quoted text -
            >>
            >- Show quoted text -
            >
            Is that just the way that PHP handles overriding (it isn't really
            overloading is it)? I don't remember that way being anything specific
            for oop. I know some have the word virtual and overrid like C#. Or
            the derived just overrides by default. I think Java does it that way.
            >
            That's the way C++, Java and SmallTalk handle it, and it's a basic
            principal of OO. I don't know how c# does it - but it wouldn't surprise
            me if it didn't follow OO principles.

            And it isn't really overriding, either - it's actually polymorphism.
            But I don't like to use too many long words because of some of the
            trolls in this newsgroup :-).

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

            Comment

            Working...