Hi all!
What do you expect from this code?
<?php
class Foo{
private $id = false;
public function getId () {
return $this->id;
}
public function setId ($id) {
return $this->id = $id;
}
}
class Bar extends Foo {
public function setId ($id) {
return $this->id = $id;
}
}
$b = new Bar;
$b->setId(123);
var_dump($b->getId());
?>
It outputs 'bool(false)', while I expect it to print 'int(123)'.
Changing private $id to public or protected solves the problem.
Adding getId() method to the Bar solves it too, but this is obviously
rather silly solution.
Am I missing something and this is really expected behaviour ?
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net || antony@dovgal.com
Hi,
This is expected since the property is marked private, meaning its only
accessable within its own scope, not for extended classes. For that you
need to use protected, as you mention.
ray
e: ray.hilton@memefeeder.com
w: http://www.memefeeder.com
Antony Dovgal wrote:
Hi all!
What do you expect from this code?
<?php
class Foo{
private $id = false;
public function getId () {
return $this->id;
}
public function setId ($id) {
return $this->id = $id;
}}
class Bar extends Foo {
public function setId ($id) {
return $this->id = $id;
}
}$b = new Bar;
$b->setId(123);
var_dump($b->getId());?>
It outputs 'bool(false)', while I expect it to print 'int(123)'.
Changing private $id to public or protected solves the problem.
Adding getId() method to the Bar solves it too, but this is obviously
rather silly solution.Am I missing something and this is really expected behaviour ?
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net || antony@dovgal.com
Hi,
that's imho expected behaviour. To quote
http://www.php.net/zend-engine-2.php
| Protected member variables can be accessed in classes extending the class
| they are declared in, whereas private member variables can only be
| accessed by the class they belong to.
And $id belongs to the base class and not to the extended. What else should
be the difference between private and protected?
johannes
Antony Dovgal wrote:
Hi all!
What do you expect from this code?
<?php
class Foo{
private $id = false;
public function getId () {
return $this->id;
}
public function setId ($id) {
return $this->id = $id;
}}
class Bar extends Foo {
public function setId ($id) {
return $this->id = $id;
}
}$b = new Bar;
$b->setId(123);
var_dump($b->getId());?>
It outputs 'bool(false)', while I expect it to print 'int(123)'.
Changing private $id to public or protected solves the problem.
Adding getId() method to the Bar solves it too, but this is obviously
rather silly solution.Am I missing something and this is really expected behaviour ?
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net || antony@dovgal.com
On Tue, 15 Jun 2004 16:32:55 +0200
Johannes Schlueter schlueter@phpbar.de wrote:
Hi,
that's imho expected behaviour. To quote
http://www.php.net/zend-engine-2.php
| Protected member variables can be accessed in classes extending the
| class they are declared in, whereas private member variables can
| only be accessed by the class they belong to.And $id belongs to the base class and not to the extended. What else
should be the difference between private and protected?
I just was baffled by the fact, that without setId() in the extended
class it works fine.
But now I got it, thanks.
And sorry for the noise.
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net || antony@dovgal.com
Antony Dovgal wrote:
It outputs 'bool(false)', while I expect it to print 'int(123)'.
Foo::id is private so Bar::setId() cannot use it. It uses an implicit
public Bar::id (different from Foo::id).
Foo::getId() correctly read the private attribute Foo::id (not the
public Bar::id) and return it.
Am I missing something and this is really expected behaviour ?
I think it is.
- if we allow Bar::setId() to change Foo::id, "private" keyword is useless
- if we allow Foo::getId() to read Bar::id instead of Foo:id, then a
derived class can pertubate private things in the mother class (which is
not a good solution).
--
Eric Daspet