By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

oop question

P: n/a
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::testPublic\n";
}

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

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

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

$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// Foo::testPublic
Sep 16 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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::testPublic\n";
* * }

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

}

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

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

}

$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
* * * * * * * * // 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.
Sep 16 '08 #2

P: n/a
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::testPublic\n";
}

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

}

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

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

}

$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// 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.
js*******@attglobal.net
==================

Sep 16 '08 #3

P: n/a
On Sep 16, 12:54*pm, Jerry Stuckle <jstuck...@attglobal.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::testPublic\n";
* * }
* * private function testPrivate() {
* * * * echo "Bar::testPrivate\n";
* * }
}
class Foo extends Bar
{
* * public function testPublic() {
* * * * echo "Foo::testPublic\n";
* * }
* * private function testPrivate() {
* * * * echo "Foo::testPrivate\n";
* * }
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
* * * * * * * * // 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...@attglobal.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.
Sep 16 '08 #4

P: n/a
..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
Sep 16 '08 #5

P: n/a
jmDesktop wrote:
On Sep 16, 12:54 pm, Jerry Stuckle <jstuck...@attglobal.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::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// 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...@attglobal.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.
js*******@attglobal.net
==================

Sep 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.