/    Sign up×
Community /Pin to ProfileBookmark

protected or private? which one to use ..

hello,

I have a class called [B]Object[/B] and another class [B]Person extends Object[/B]

in the base class i want to use members (properties) which can be used in the Person class

[code=php]class Object {
protected $_number1;
private $_number2;
{

class Person extends Object{
public function __construct($n1,$n2){
$this->_number1 = $n1;
$this->_number2 = $n2;
}
}[/code]

properies number1 and number2 are both accessible!! so what is difference?

Thank u
kind regards

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 14.2009 — Public: anyone either inside the class or outside can access them

Private: only the specified class can access them. Even subclasses will be denied access.

Protected: only the specified class and subclasses can access them

You are clearly encountering some form of bug, more information and a sample of how you ran into this would be nice.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — Remember that PHP allows you to set undefined class variables at run-time. So when you set the $_number2 variable in the child class, it is being created at that time as a public variable. To see what I mean, run this:
[code=php]
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

class foo
{
public $var1 = 1;
protected $var2 = 2;
private $var3 = 3;
}

class bar extends foo
{
public function test()
{
printf("&#37;d<br>%d</br>%d", $this->var1, $this->var2, $this->var3);
}

public function set()
{
$this->var3 = 33;
}
}

$obj = new bar();
echo "<p>".$obj->test()."</p>"; // will generate notice-level warning
$obj->set();
echo "<p>".$obj->test()."</p>";
[/code]

Output:


Notice: Undefined property: bar::$var3 in C:wampwwwtesttest.php on line 16

1

2

0 [color=blue][variable was undefined, so ouput as 0 instead of 3 by printf()][/color]

1

2

33 [color=blue][now it is set (at run-time) as a public variable][/color]
[/quote]
Copy linkTweet thisAlerts:
@scragarFeb 14.2009 — Didn't know it created new variables when you tried to set them, that's intresting.
Copy linkTweet thisAlerts:
@NogDogFeb 14.2009 — It's one of those things that is both a feature and a bug, depending on how you want to look at it. ?
Copy linkTweet thisAlerts:
@FinbanonauthorFeb 15.2009 — thank u

but after I added [code=php]ini_set('display_errors', 1);
error_reporting(E_ALL);[/code]

no changes happened

both variables act as they are both [B]protected [/B]!!

thanks anyway

ps: i don't use any kind of IDE

just notepad and IE browser
Copy linkTweet thisAlerts:
@NogDogFeb 15.2009 — Setting the value does not cause a notice. It only generated a notice in my sample script because I tried to echo it before ever setting it, demonstrating that at that point in the script there was no such variable in that object. Then after I set it, it did exist, and the second echo did not generate a notice.

In other words, if you assign a value to a class variable that does not exist, then it is created at that time. In your script, since the child class did not inherit the private variable, it did not exist at all in the object instantiated from that child class. It is not a case of the variable existing but not being accessible, it does not exist at all in the child class. So when you assign a value to it, it is created in that object at that moment as an object variable with public access.
Copy linkTweet thisAlerts:
@scragarFeb 15.2009 — There's an easy way to prove it:
[code=php]
class foo
{
public $var1 = 1;
protected $var2 = 2;
private $var3 = 3;
public function __set($name, $val){
if( isset($this->$name) )
$this->$name = $val;
else
throw new Exception("'{$name}' is not a supported attribute");
}
}
class bar extends foo
{
public function test()
{
printf("&#37;d<br>%d</br>%d", $this->var1, $this->var2, $this->var3);
}

public function set()
{
$this->var3 = 33;
}
}

$obj = new bar();
echo "<p>".$obj->test()."</p>"; // will generate notice-level warning
$obj->set();
echo "<p>".$obj->test()."</p>";
[/code]
×

Success!

Help @Finbanon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...