Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 18th, 2012, 01:57 AM
FlyingBeetroot FlyingBeetroot is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 73 FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 24 m 52 sec
Reputation Power: 35
Facebook
Tutorial on Class function visibility (that public/protected/private dilemma)

You can check out it here at flying beetroot

Understanding OO Visibility - PHP

Enjoy!

Reply With Quote
  #2  
Old May 18th, 2012, 06:35 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,054 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 5 h 27 m 59 sec
Reputation Power: 7104
Your tutorial is well written and does a good job of explaining those concepts.

Just because I'm OCD and my life revolves around PHP I'm going to point out two things:

(1) A constructor actually doesn't need to be public. In fact, there is a particular use-case where you would not want the constructor to be public:
PHP Code:
class special_class
    
{
        public static function 
factory_method()
        {
            return new 
self;
        }
        protected function 
__construct()
        {
            echo 
"constructed";
        }
    }
    
$inst special_class::factory_method(); 

Declaring the constructor as protected in this case prevents me from bypassing the factory method.

(2) You can actually call a protected or private method from an instantiation inside the class that contains the functions. In fact, the instantiation doesn't even need to be inside the class containing the functions. A protected or private method can be called on any object from any context that has visibility of those methods. Meaning that even something like this is legal syntax:
PHP Code:
class special_class_a
    
{
        public function 
pub($arg)
        {
            
$arg->pro();
        }
        protected function 
pro()
        {
            echo 
"pro-a";
        }
    }
    
    class 
special_class_b extends special_class_a
    
{
        protected function 
pro()
        {
            echo 
"pro-b";
        }
    }
    
    
$inst1 = new special_class_a;
    
$inst2 = new special_class_b;
    
    
$inst1->pub($inst2); 
Comments on this post
FlyingBeetroot agrees: Thanks mate - I'll update accordingly
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old May 19th, 2012, 03:13 AM
FlyingBeetroot FlyingBeetroot is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 73 FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 24 m 52 sec
Reputation Power: 35
Facebook
Thanks mate - I did know about the factory idea; but all the code I've seen uses die() in the public constructor to prevent standard instantiation.

As for point 2 - valid argument (in fact I wrote a router class for URL parsing that does this) - but it's slightly different in nature to the example I gave. Nonetheless; given it is around the same lines; I will update my tutorial to include this exception.

Cheers for taking the time to read it though Greatly appreciated.

Reply With Quote
  #4  
Old May 19th, 2012, 04:08 AM
FlyingBeetroot FlyingBeetroot is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 73 FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level)FlyingBeetroot User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 24 m 52 sec
Reputation Power: 35
Facebook
OK, it's updated and I think with your comments incorporated it's a lot better. Thanks again.

Reply With Quote
  #5  
Old May 19th, 2012, 10:43 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,054 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 5 h 27 m 59 sec
Reputation Power: 7104
Quote:
As for point 2 - valid argument (in fact I wrote a router class for URL parsing that does this) - but it's slightly different in nature to the example I gave. Nonetheless; given it is around the same lines; I will update my tutorial to include this exception.

Ah, actually my example using extension was meant to show a "most complicated" situation. It works with other cases too, such as passing an instance of special_class_a to pub() or constructing an instance of "self" inside pub instead of using the argument. At least under PHP 5.3, the tutorial's example of self instantiation executes correctly with no errors.

However, this is sort of an edge case as objects are rarely used in this way.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Tutorial on Class function visibility (that public/protected/private dilemma)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap