TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   OOP-related keywords (http://www.talkphp.com/advanced-php-programming/1786-oop-related-keywords.html)

reborn 12-21-2007 02:32 PM

OOP-related keywords
 
Can anyone explain to me exactly how keywords like "public", "private" and so on works when I write classes?

sketchMedia 12-21-2007 03:12 PM

They are visibilty identifiers
:
Visibility is similar to variable scope, however, offers finer control. There are three types of visibility.
  • Public (default) - the variable can be accessed and changed globally by anything.
  • Protected - the variable can be accessed and changed only by direct descendants (those who inherit it using the extends statement) of the class and class its self
  • Private - the variable can be accessed and changed only from within the class.

Village Idiot 12-21-2007 03:39 PM

As taken from http://tips.justanotherportfolio.com/?page_id=6
Visibility
Variables in classes can be set to three different types of visibility Visibility tells the program who can see what. The three types are:
Public: Anyone can access it, outside, inside and child classes.
Example: Any above code, anything can access it.

Protected: Only functions within that class and child classes can access it
example:
<?
class calc_functions
{
protected function add($x,$y)
{
return $x + $y;
}
protected function subtract($x,$y)
{
return $x - $y;
}
}
class calc extends calc_functions
{
public function display()
{
echo “100 + 100 is “.parent::add(100,100).”<br />200 - 100 is “.parent::subtract(200,100);
}
}
$math = new calc;
$math->display();
?>
As you can see, the function it extended to are able to access calc_function’s functions, but if we where to make an instance of calc_function and try to call $instance->add(100,100); you would get a fatal error.

Private: Only functions within that class can access it.
Example:
<?
class calc
{
protected function add($x,$y)
{
return $x + $y;
}
protected function subtract($x,$y)
{
return $x - $y;
}
public function display()
{
echo “100 + 100 is “.calc::add(100,100).”<br />200 - 100 is “.calc::subtract(200,100);
}
}
$math = new calc;
$math->display();
?>
As you can see here, those could be accessed by the functions within the class, if we where to create a child class to inherit it, neither of those functions could be accessed by it. The same applies to outside the class.

Note:
When using var to declare a variable in a class, it sets it to public, this was only continued from php4 (which didn’t have visibility settings) for code compatibility reasons. It is deprecated and probably shouldn’t be used, but it doesn’t make a difference unless you need to use private or protected. Functions left without a visibility definition are public, I generally don’t define public in my code, it doesn’t make a difference.

reborn 12-21-2007 03:52 PM

Ok, thanks. I think I understand now. :)

Salathe 12-21-2007 05:06 PM

Just to comment on Village Idiot's article, scope is something different (but obviously related) to class method/property visibility. Your article is talking about the latter. I only post this message in case people read it and get the wrong (well, an inaccurate) impression.

Village Idiot 12-21-2007 08:06 PM

And there I go again with C++ terms.... I'll revise that.


All times are GMT. The time now is 02:18 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0