You are here: Re: php 5 classes: public, protected and private « PHP Programming Language « IT news, forums, messages
Re: php 5 classes: public, protected and private

Posted by Jerry Stuckle on 12/03/06 15:10

Curtis wrote:
>>That's just your opinion. Where does it say that I *MUST* define and use an
>>interface before I can access a class method? Interfaces are optional
>>(especuially in PHP) so it is not wrong to excercise the option NOT to use
>>them. I can define a class method and access that method without using an
>>interface, and that is what I choose to do.
>
>
> No one is saying that you have to use interfaces. The point is that it
> is there to help in organizing and creating classes, the same goes for
> visibility keywords. Just because you don't have to use these, doesn't
> mean that you should not use them.
>
> I can see an advantage in using interfaces and/or visibility keywords
> in PHP, if one is creating a large library, or to help communications
> between a team of developers. These features can help track down where
> a problem is if something isn't working right, or just for clarifying
> the particular usage for the class or its members.
>
> BTW, I do not believe there is any controversy over what encapsulation
> is; if so, I haven't heard about it.
>

Hi, Curtis,

Actually, there is. Tony is claiming that variables should not be
private because they are not part of the implementation.

All of the industry-recognized experts, college level OOAD courses, etc.
disagree with him. So do the people who designed Java.

And even the PHP designers disagree with him - otherwise why would they
have bothered adding private and protected to variables, for instance?

Data should be private because it hides how the data is being stored. A
SQL database is a perfect example. You can't access the data directly,
but you can do it through SQL. And even if the internal representation
of the data changes (i.e. switch from MyISAM to InnoDB engines in MySQL,
or upgrade MySQL), you can still access the data with no changes to the
program. The data is encapsulated, but it is not hidden (it's all
available).

From one of the examples Tony quoted for an example of "proper" OO
coding. The user is storing:

public $longitude = 12.3456;
public $latitude = 23.4567;

The problem with having public variables is you may need to change them
later. There are other ways of storing this data, i.e.

public $longdeg = 12;
public $longmin = 34;
public $longsec = 56;

Or
public $longitude = array('deg'=>12, 'min'=>34, 'sec'=>56);

Or
public $longitude = 45678; // number of seconds
Or
public $longitude = "12.34.56"

and so on.

If these variables are public, you can never change them. But what
happens if you need to - i.e. for performance? Or accuracy?

For instance, if your database stores the long/lat in "12.34.56" format,
and all you need to do is display it, converting to and from
degrees/decimal is unnecessary and inefficient.

Or what if your database is stored as number of seconds, and you just
need to pull out the degrees, minutes and seconds? Again, unnecessary
overhead.

Rather, if you encapsulate the longitude and latitude, making them
private, with accessor functions, the data is not hidden - but the
implementation is.

This is where the disagreement lies. And it's why Tony is wrong.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация