473,854 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php 5 classes: public, protected and private

Hi,

finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?

I quote the php manual: "The visibility of a property or method can be
defined by prefixing the declaration with the keywords: public,
protected or private. Public declared items can be accessed
everywhere."

But should I read "...can be accessed everywhere within a given class."
or "...can be accessed by all other classes." ?

Job

Nov 27 '06 #1
86 4685
jopperdepopper wrote:
Hi,

finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?

I quote the php manual: "The visibility of a property or method can be
defined by prefixing the declaration with the keywords: public,
protected or private. Public declared items can be accessed
everywhere."

But should I read "...can be accessed everywhere within a given class."
or "...can be accessed by all other classes." ?

Job
Job,

You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 27 '06 #2
>
You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

Thanks Jerry. I'm trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the 'why'
behind the 'public, protected and private' and stuff like abstraction,
interfaces and whatnot. Feels like things are being over-complicated
somehow... or it's just my being inexperienced on this...

Any other reading material on this suggested, someone?

Nov 27 '06 #3
jopperdepopper wrote:
>>You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

Thanks Jerry. I'm trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the 'why'
behind the 'public, protected and private' and stuff like abstraction,
interfaces and whatnot. Feels like things are being over-complicated
somehow... or it's just my being inexperienced on this...

Any other reading material on this suggested, someone?
Look for some good books on OO theory and design.

Two of the concepts in OO are 'encapsulation' and 'methods'.

Encapsulation means the internals of an object are managed only by that
object and are not available to anyone else. In PHP these are private
members.

Methods are implemented as functions in PHP. They operate on the object.

The idea is to completely separate the implementation of the class from
the use of it. It means you can change the class any way you want. As
long as the public interface remains the same, it makes no difference.

A good example of this is floating point numbers. They are stored as a

x * 2^y

X is the mantissa, y is the base 2 exponent. For instance, 8 would be
stored as x=1 and y=3, because 8 is 1 * 2^3. But all of this is hidden
behind the scenes; you can do operations on a floating point number such
as add, subtract, print, etc.

And if at some other time PHP changed the implementation to something
else, none of your code would change.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 27 '06 #4
Look for some good books on OO theory and design.
Thanks Jerry, maybe the php 5 approach is a better implementation than
the php 4 approach? I have always felt that in php 4 using classes (or
at least my way of using them) was more a convenient way to organise
code, grouping some related functions in one class, other related ones
in another...

Gotta get me some books & hope it's going to be a loooong coooold
winter ;)

Nov 27 '06 #5
jopperdepopper wrote:
>>Look for some good books on OO theory and design.


Thanks Jerry, maybe the php 5 approach is a better implementation than
the php 4 approach? I have always felt that in php 4 using classes (or
at least my way of using them) was more a convenient way to organise
code, grouping some related functions in one class, other related ones
in another...

Gotta get me some books & hope it's going to be a loooong coooold
winter ;)
Yes, I think it is - but then I've been doing OO programming for a
number of years, both in C++ and Java.

Classes are a good way to organize code - but more importantly, they are
a way of organizing code AND DATA. A properly constructed class should,
as much as possible, manage it's own data independent of other classes
and code.

It's a whole different way of thinking which is usually quite a jump for
experienced programmers. In fact, I find newer programmers typically
have less problems, because structured code techniques are not so deeply
ingrained in their mind. :-)

But it's well worth it; the resulting code can be much more readable and
maintainable.

A good example. I needed to implement some pages based on a database.
However, the particular host being used at the time did not have MySQL
available (they claimed they did, but it wasn't very reliable...).

So I implemented the code in flat files using a class for the data being
displayed, and pages to use that class. Later, when they changed to a
host which had MySQL, all I had to do was go back to the class and
change it to read from a database instead of a flat file. No changes to
the pages were needed at all. Very clean and easy to do, because I
segregated the operations on the data in the class, and used the web
page code just to display the data.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 27 '06 #6

"Jerry Stuckle" <js*******@attg lobal.netwrote in message
news:Ib******** *************** *******@comcast .com...
jopperdepopper wrote:
>>>You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

Thanks Jerry. I'm trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the 'why'
behind the 'public, protected and private' and stuff like abstraction,
interfaces and whatnot.
Intefaces are not necessary in PHP. Once you have defined a method it is a
total waste to time to also define an interface. Interfaces are a "fix" in
those languages as a means of dealing with optional arguments and statyic
typing. PHP has ifferent ways of dealing with bth of these, therefore
interfaces serve no useful purpose.
>Feels like things are being over-complicated
somehow... or it's just my being inexperienced on this...

Any other reading material on this suggested, someone?

Look for some good books on OO theory and design.

Two of the concepts in OO are 'encapsulation' and 'methods'.

Encapsulation means the internals of an object are managed only by that
object and are not available to anyone else. In PHP these are private
members.
Wrong. Encapsulation means that the data and the functions which operate on
that data are contained (encapsulated) within a single object. While the
methods (functions) thenselves may be visible the code behind those methods
(i.e. the implementaton behind those methods) is not. Encapslation is NOT
about hiding information, it is about hiding the implementation. It is not
necessary to use public/private/protected on any methods or properties. It
does not add any functionality, it merely creates restrictions which often
get in the way.
Methods are implemented as functions in PHP. They operate on the object.
That's one thing you got right.

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Nov 27 '06 #7
..oO(Tony Marston)
>Intefaces are not necessary in PHP. Once you have defined a method it is a
total waste to time to also define an interface. Interfaces are a "fix" in
those languages as a means of dealing with optional arguments and statyic
typing. PHP has ifferent ways of dealing with bth of these, therefore
interfaces serve no useful purpose.
What's called an interface in PHP is a completely different mechanism
than what you described above.
>Wrong. Encapsulation means that the data and the functions which operate on
that data are contained (encapsulated) within a single object. While the
methods (functions) thenselves may be visible the code behind those methods
(i.e. the implementaton behind those methods) is not. Encapslation is NOT
about hiding information, it is about hiding the implementation. It is not
necessary to use public/private/protected on any methods or properties.
Of course it is necessary.
>It
does not add any functionality, it merely creates restrictions which often
get in the way.
It prevents developers from doing things that shouldn't be done, for
example calling an internal method out of context. I don't want all my
methods being publicly available, simply in order to avoid errors and
unpredictable results.

Micha
Nov 27 '06 #8
Tony Marston wrote:
"Jerry Stuckle" <js*******@attg lobal.netwrote in message
news:Ib******** *************** *******@comcast .com...
>>jopperdepoppe r wrote:
>>>>You should read "can be accessed everywhere".

Private members can be accessed by members of the class only.
Protected members can be accessed by members of the class or a derived
class.
Public members can be accessed by anyone, including other classes,
functions and any other code.

Thanks Jerry. I'm trying to make a bit of sense of the php 5 approach
to classes, and so far having a hard time. I fail to see the 'why'
behind the 'public, protected and private' and stuff like abstraction,
interfaces and whatnot.


Intefaces are not necessary in PHP. Once you have defined a method it is a
total waste to time to also define an interface. Interfaces are a "fix" in
those languages as a means of dealing with optional arguments and statyic
typing. PHP has ifferent ways of dealing with bth of these, therefore
interfaces serve no useful purpose.
Ah, the great Tony Marston is back to trolling again.

Wrong. In OO terms, the interface is the way to interact with the
object. It consists of all public members - both methods (functions, in
PHP) and variables. And for derived classes, the base class adds
protected members.

A PHP interface is something entirely different.
>
>>>Feels like things are being over-complicated
somehow... or it's just my being inexperienced on this...

Any other reading material on this suggested, someone?

Look for some good books on OO theory and design.

Two of the concepts in OO are 'encapsulation' and 'methods'.

Encapsulati on means the internals of an object are managed only by that
object and are not available to anyone else. In PHP these are private
members.


Wrong. Encapsulation means that the data and the functions which operate on
that data are contained (encapsulated) within a single object. While the
methods (functions) thenselves may be visible the code behind those methods
(i.e. the implementaton behind those methods) is not. Encapslation is NOT
about hiding information, it is about hiding the implementation. It is not
necessary to use public/private/protected on any methods or properties. It
does not add any functionality, it merely creates restrictions which often
get in the way.
Wrong again, Tony. Encapsulation means internal representations of the
object are not visible outside of the class. Just like the base &
mantissa are not visible outside of a floating point number.
>
>>Methods are implemented as functions in PHP. They operate on the object.


That's one thing you got right.
More than you got right. Go crawl back into your hole and don't come
out again until you know what you're talking about.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Nov 28 '06 #9

"Michael Fesser" <ne*****@gmx.de wrote in message
news:fd******** *************** *********@4ax.c om...
.oO(Tony Marston)
>>Intefaces are not necessary in PHP. Once you have defined a method it is a
total waste to time to also define an interface. Interfaces are a "fix" in
those languages as a means of dealing with optional arguments and statyic
typing. PHP has ifferent ways of dealing with bth of these, therefore
interfaces serve no useful purpose.

What's called an interface in PHP is a completely different mechanism
than what you described above.
I disagree. It is possible to define a function (method) within a class,
then to define a separate thng called an "interface" . It is possible access
he function without using the interface, therefore the inteface is not
necessary.
>>Wrong. Encapsulation means that the data and the functions which operate
on
that data are contained (encapsulated) within a single object. While the
methods (functions) thenselves may be visible the code behind those
methods
(i.e. the implementaton behind those methods) is not. Encapslation is NOT
about hiding information, it is about hiding the implementation. It is not
necessary to use public/private/protected on any methods or properties.

Of course it is necessary.
I disagree. it is *not* necessary for the simple reason that the code will
perform exactly the same function whether methods and properties are marked
as public/private/protected or not.
>>It
does not add any functionality, it merely creates restrictions which often
get in the way.
That is why I said "It does not add any functionality, it merely creates
restrictions"
It prevents developers from doing things that shouldn't be done, for
example calling an internal method out of context. I don't want all my
methods being publicly available, simply in order to avoid errors and
unpredictable results.
That is a matter for programmer discipline, it is not a matter of additional
functionality. The code will do exactly the same with or without it.

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Nov 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1813
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number; virtual long myfunc(void); }
45
3638
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
3
2068
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about "friend to class". I am very disappointed because it is the way how C++ Compiler is designed. I assume that "friend to class" is not the good option. If CMain class is initialized before CA class, CB class, and CC class are initialized inside CMain...
14
3811
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
6
3370
by: steve bull | last post by:
I created a usercontrol class, RGBColorSpace, which is derived from an abstract class, ColorSpace, but when I try to click on the design panel for the control I get an error message "Unable to create instance of abstract class ColorSpace". I never try to create a class ColorSpace and don't really want to create a regular class with virtual methods. Does anyone know why I am having this problem? The code seems to run fine and I can actually...
11
3855
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
5
2011
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1" Gpa="3.50"> <Person Id="1" FirstName="FirstName0" LastName="LastName0" MiddleInitial="W"/> </Student> In my code, person is the base class and student extends it. When I
2
2376
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is when a nested class inherits from it’s parent class you get this infinite class chain in intellisense when consuming the class. To get around this I created two child classes Reader and Writer which require a base Person object. When consuming...
47
4055
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10685
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9518
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5750
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4162
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.