TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-21-2007, 02:32 PM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 3
reborn is on a distinguished road
Default OOP-related keywords

Can anyone explain to me exactly how keywords like "public", "private" and so on works when I write classes?
reborn is offline  
Reply With Quote
Old 12-21-2007, 03:12 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 12-21-2007 at 03:37 PM. Reason: copied it from my tutorial and left some stuff in that wasnt needed
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
reborn (12-21-2007)
Old 12-21-2007, 03:39 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________


Last edited by Village Idiot : 12-21-2007 at 08:08 PM.
Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
reborn (12-21-2007)
Old 12-21-2007, 03:52 PM   #4 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 3
reborn is on a distinguished road
Default

Ok, thanks. I think I understand now. :)
reborn is offline  
Reply With Quote
Old 12-21-2007, 05:06 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 12-21-2007, 08:06 PM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design