# Weird behaviour of private attribute + overloaded method

- Thread: https://externals.io/message/10468
- Original archive: https://news-web.php.net/php.internals/10468
- Started by Antony Dovgal on 2004-06-15T13:59:18+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10468 — Antony Dovgal — 2004-06-15T13:59:18+00:00

URL: https://externals.io/message/10468#10468

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

---

## #10472 — Ray Hilton — 2004-06-15T14:11:04+00:00

URL: https://externals.io/message/10468#10472 · In reply to: #10468

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

---

## #10469 — Johannes Schlueter — 2004-06-15T14:32:55+00:00

URL: https://externals.io/message/10468#10469 · In reply to: #10468

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

---

## #10471 — Eric Daspet — 2004-06-15T14:35:06+00:00

URL: https://externals.io/message/10468#10471 · In reply to: #10468

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

---

## #10470 — Antony Dovgal — 2004-06-15T14:38:16+00:00

URL: https://externals.io/message/10468#10470 · In reply to: #10469

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
