php-general Digest 9 Oct 2007 14:13:40 -0000 Issue 5063
Topics (messages 262946 through 262967):
Re: How to format CLI tabular data?
262946 by: Robert Cummings
262947 by: Larry Garfield
262948 by: Jim Lucas
262957 by: tedd
262966 by: Richard Heyes
Re: php5 - possible bug discovered
262949 by: David Restall - System Administrator
262950 by: Stut
Chmod a Directory
262951 by: abderrazzak nejeoui
262952 by: Samuel Vogel
262954 by: Stut
262964 by: Daniel Brown
Re: Sending lots of emails - 2 choices - choose the best one
262953 by: Stut
262960 by: Daniel Brown
262963 by: marek
Re: Structure of maintainable websites
262955 by: Richard Heyes
Re: Beginner Tutorials for using CLASSES in PHP4
262956 by: Nathan Nobbe
262959 by: Nathan Nobbe
262961 by: Nathan Nobbe
262962 by: Nathan Nobbe
262965 by: Tony Marston
262967 by: Nathan Nobbe
Re: Something you can do with AJAX + PHP as well
262958 by: Martin Zvarík
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
On Mon, 2007-10-08 at 15:59 -0700, Daevid Vincent wrote:
> I write many CLI scripts in PHP to clean up DB records, etc via crontab
> scripts. Often though, I implement command line switches to 'debug' or see
> various data as a precaution before actually purging.
>
> Perl has some neat tools built in to format tabular data:
> http://www.usenix.org/publications/perl/perl08.html
>
> Does PHP have any tools like this? I'd prefer not to pull in some PEAR
> package or other bloat, but I'll take what I can get if it actually works
> well. I was hoping there are some tips/tricks to show simple tabular data:
>
> i.e.:
>
> # foo.php --showgroups
>
> ==================================
> Groups
> ==================================
> Name: Expires: Date:
> -------- ------------ -----------
> groupA 3 hours 2007-10-08
> groupBeta 10 hours 2007-11-10
> groupC 1 week 2007-12-31
>
>
> notice basic things like alignment,
> length of data (as in 'tabs' won't work), etc.
Looks like something you could whip up in half an hour.
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--- End Message ---
--- Begin Message ---
Look into sprintf(). I believe it has whitespace formatting codes that are
wonderfully suited to this case. (They're derived from C, where the printf()
routine is used quite frequently for that sort of screen output.)
On Monday 08 October 2007, Robert Cummings wrote:
> On Mon, 2007-10-08 at 15:59 -0700, Daevid Vincent wrote:
> > I write many CLI scripts in PHP to clean up DB records, etc via crontab
> > scripts. Often though, I implement command line switches to 'debug' or
> > see various data as a precaution before actually purging.
> >
> > Perl has some neat tools built in to format tabular data:
> > http://www.usenix.org/publications/perl/perl08.html
> >
> > Does PHP have any tools like this? I'd prefer not to pull in some PEAR
> > package or other bloat, but I'll take what I can get if it actually works
> > well. I was hoping there are some tips/tricks to show simple tabular
> > data:
> >
> > i.e.:
> >
> > # foo.php --showgroups
> >
> > ==================================
> > Groups
> > ==================================
> > Name: Expires: Date:
> > -------- ------------ -----------
> > groupA 3 hours 2007-10-08
> > groupBeta 10 hours 2007-11-10
> > groupC 1 week 2007-12-31
> >
> >
> > notice basic things like alignment,
> > length of data (as in 'tabs' won't work), etc.
>
> Looks like something you could whip up in half an hour.
>
> Cheers,
> Rob.
> --
> ...........................................................
> SwarmBuy.com - http://www.swarmbuy.com
>
> Leveraging the buying power of the masses!
> ...........................................................
--
Larry Garfield AIM: LOLG42
[EMAIL PROTECTED] ICQ: 6817012
"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
--- End Message ---
--- Begin Message ---
Daevid Vincent wrote:
I write many CLI scripts in PHP to clean up DB records, etc via crontab
scripts. Often though, I implement command line switches to 'debug' or see
various data as a precaution before actually purging.
Perl has some neat tools built in to format tabular data:
http://www.usenix.org/publications/perl/perl08.html
Does PHP have any tools like this? I'd prefer not to pull in some PEAR
package or other bloat, but I'll take what I can get if it actually works
well. I was hoping there are some tips/tricks to show simple tabular data:
i.e.:
# foo.php --showgroups
==================================
Groups
==================================
Name: Expires: Date:
-------- ------------ -----------
groupA 3 hours 2007-10-08
groupBeta 10 hours 2007-11-10
groupC 1 week 2007-12-31
notice basic things like alignment,
length of data (as in 'tabs' won't work), etc.
Try this out
<plaintext><?php
$data[] = array('one', 'two', 'three');
$data[] = array('oneoneone', 'two', 'three');
$data[] = array('oneone', 'two', 'threeone');
$data[] = array('one', 'onetwo', 'three');
$data[] = array('one', 'two', 'oneoneonethree');
$data[] = array('one', 'two', 'three');
function displayFixedWidth($data) {
$seperation = 3;
$seperator = '=';
$columnWidths = array();
foreach($data AS $header=>$row) {
foreach($row AS $column => $value) {
$columnWidths[$column] = max(strlen($value),
@$columnWidths[$column]);
}
}
$totalWidth = 0;
foreach($columnWidths AS $length) {
$totalWidth += ($length+$seperation);
}
echo str_pad('', ($totalWidth+count($columnWidths)), $seperator)."\n";
foreach($columnWidths AS $header => $length) {
echo ' '.str_pad($header, ($length+$seperation), ' ');
}
echo "\n".str_pad('', ($totalWidth+count($columnWidths)),
$seperator)."\n";
foreach($data AS $header=>$row) {
foreach($row AS $column => $value) {
echo ' '.str_pad($value, ($columnWidths[$column]+$seperation));
}
echo "\n";
}
echo str_pad('', ($totalWidth+count($columnWidths)), $seperator)."\n";
}
displayFixedWidth($data);
--
Jim Lucas
"Perseverance is not a long race;
it is many short races one after the other"
Walter Elliot
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--- End Message ---
--- Begin Message ---
At 3:59 PM -0700 10/8/07, Daevid Vincent wrote:
I write many CLI scripts in PHP to clean up DB records, etc via crontab
scripts. Often though, I implement command line switches to 'debug' or see
various data as a precaution before actually purging.
Perl has some neat tools built in to format tabular data:
http://www.usenix.org/publications/perl/perl08.html
Does PHP have any tools like this? I'd prefer not to pull in some PEAR
package or other bloat, but I'll take what I can get if it actually works
well. I was hoping there are some tips/tricks to show simple tabular data:
i.e.:
# foo.php --showgroups
==================================
Groups
==================================
Name: Expires: Date:
-------- ------------ -----------
groupA 3 hours 2007-10-08
groupBeta 10 hours 2007-11-10
groupC 1 week 2007-12-31
notice basic things like alignment,
length of data (as in 'tabs' won't work), etc.
It's tabular -- why not use tables?
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
Daevid Vincent wrote:
I write many CLI scripts in PHP to clean up DB records, etc via crontab
scripts. Often though, I implement command line switches to 'debug' or see
various data as a precaution before actually purging.
Perl has some neat tools built in to format tabular data:
http://www.usenix.org/publications/perl/perl08.html
Does PHP have any tools like this?
The PEAR class Console_Table will do this for you.
I'd prefer not to pull in some PEAR
package or other bloat
PEAR does not automatically mean bloat. It does however automatically
mean less work for you.
--
Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk
Knowledge Base and HelpDesk software
that can cut the cost of online support
--- End Message ---
--- Begin Message ---
Hi Robert, Stut & List
Thanks for your replies which arrived after I had gone to bed |-Z
Robert :-
> In PHP5 objects are no longer copied when assigned. Instead the object's
> handle is assigned (similar to a reference but not quite). So the
> behaviour is as expected.
Stut :-
> > I think I have discovered a bug in php5. If I haven't, I've discovered
> > a bug in the documentation or a bug in my brain.
>
> Start here: http://php.net/language.oop5.cloning
It's amazing what a good night's sleep will do :-)
I could not find that page last night even though I was sure it must
have existed and I can see that it is a well documented and encountered
problem.
My main obesrvation is that I'm glad that I decided to revisit all my
old PHP4 stuff and rewrite or clean it up. I have hundreds of
$Working_Class = $Under_Class;
lines in my code as well as
$Upper_Class = & $Aristocracy;
lines too. I think the documentation on references needs updating to
reflect PHP 5's behaviour a little more accurately because I can see
this causing serious problems to somebody just copying their code blindly.
I have just built a client a development server using php5 instead of
php4 and turned zend.ze1_compatibility_mode on so that their old CMS would
work - I just hadn't realised that the cloning problem was solved by this.
Oh well, now I'm fresh I can crack on :-)
TTFN
D
php/general-2007-10-09.tx [EMAIL PROTECTED]
[EMAIL PROTECTED]
php-general
+----------------------------------------------------------------------------+
| Dave Restall, Computer Nerd, Cyclist, Radio Amateur G4FCU, Bodger |
| Mob +44 (0) 7973 831245 Skype: dave.restall Radio: G4FCU |
| email : [EMAIL PROTECTED] Web : Not Ready Yet :-( |
+----------------------------------------------------------------------------+
| birth, n: |
| The first and direst of all disasters. |
| -- Ambrose Bierce, "The Devil's Dictionary" |
+----------------------------------------------------------------------------+
--- End Message ---
--- Begin Message ---
David Restall - System Administrator wrote:
My main obesrvation is that I'm glad that I decided to revisit all my
old PHP4 stuff and rewrite or clean it up. I have hundreds of
$Working_Class = $Under_Class;
lines in my code as well as
$Upper_Class = & $Aristocracy;
lines too. I think the documentation on references needs updating to
reflect PHP 5's behaviour a little more accurately because I can see
this causing serious problems to somebody just copying their code blindly.
This change was made very clear in the changelog for PHP5. Whenever you
change to a different PHP version the changelog should be the first
thing you read. If you try to read the documentation to pick out the
changes you'll miss something.
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
Please how can i chmod a directory to 0777
i tried chmod ($myDirectory, 0777); but nothing happens
thanks in advence
Nejeoui
--- End Message ---
--- Begin Message ---
You will have to loop through the directory recursively, running chmod()
on every file there is!
Regards,
Samy
abderrazzak nejeoui schrieb:
Please how can i chmod a directory to 0777
i tried chmod ($myDirectory, 0777); but nothing happens
thanks in advence
Nejeoui
--- End Message ---
--- Begin Message ---
abderrazzak nejeoui wrote:
Please how can i chmod a directory to 0777
i tried chmod ($myDirectory, 0777); but nothing happens
Check the return value. If it's false then it's failing for some
reason., most likely because the user it's running as doesn't have
permission to extend the permissions on that directory that far.
Also make sure you read the notes here: http://php.net/chmod
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
On 10/9/07, Stut <[EMAIL PROTECTED]> wrote:
> abderrazzak nejeoui wrote:
> > Please how can i chmod a directory to 0777
> > i tried chmod ($myDirectory, 0777); but nothing happens
>
> Check the return value. If it's false then it's failing for some
> reason., most likely because the user it's running as doesn't have
> permission to extend the permissions on that directory that far.
>
> Also make sure you read the notes here: http://php.net/chmod
>
> -Stut
>
> --
> http://stut.net/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Once again, Stut is most likely correct. Chances are the
directory was created by someone other than as whom the script is
being run. If you're server isn't configured to use phpSuExec, sticky
bits, or a similar setup, then chances are that you created the
directory as your user account (via FTP, SSH, a file manager, etc.),
and are attempting to chmod() the directory as Apache's user (nobody,
httpd, daemon, apache, etc.).
If that's the case, your best bet is to remove the directory using
the same medium with which you created it (if you can), and add the
following above your chmod() line:
<?
$directory = "myDirName";
if(!is_dir($directory)) mkdir($directory);
chmod($directory,0777);
?>
However, another point to keep in mind is that, if you do things
correctly, you should never need a directory to be 0777 (everyone can
read, write, and execute), as that's a serious potential security
risk.
--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107
Give a man a fish, he'll eat for a day. Then you'll find out he was
allergic and is hospitalized. See? No good deed goes unpunished....
--- End Message ---
--- Begin Message ---
Philip Thompson wrote:
On 10/7/07, Stut <[EMAIL PROTECTED]> wrote:
I would recommend avoiding the use of BCC from PHP.
Why avoid Bcc from PHP?
Note that this applies to all automated sending, not just from PHP.
It depends on how it's sending the mail. I've come across configurations
where the first mail server it hits refuses to send it because there are
so many BCC recipients.
You are also more likely to fall foul of antispam systems because the To
address is not the same as the address the recipient.
IMHO the only legitimate use for BCC in an automated system is to have a
copy sent to you for debugging/monitoring purposes.
Of course this is just my experience-based opinion.
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
On 10/9/07, Stut <[EMAIL PROTECTED]> wrote:
> Philip Thompson wrote:
> > On 10/7/07, Stut <[EMAIL PROTECTED]> wrote:
> >> I would recommend avoiding the use of BCC from PHP.
> >
> > Why avoid Bcc from PHP?
>
> Note that this applies to all automated sending, not just from PHP.
>
> It depends on how it's sending the mail. I've come across configurations
> where the first mail server it hits refuses to send it because there are
> so many BCC recipients.
>
> You are also more likely to fall foul of antispam systems because the To
> address is not the same as the address the recipient.
>
> IMHO the only legitimate use for BCC in an automated system is to have a
> copy sent to you for debugging/monitoring purposes.
>
> Of course this is just my experience-based opinion.
>
> -Stut
>
> --
> http://stut.net/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Also keep in mind that some buggy or misconfigured mail systems
have been known to forward the BCC data as an "X-Apparently-To" or
"Delivered-To" tag, even if the message is not 'apparently to' you.
In fact, I even used it years back in a honeypot server. So if it's
important to keep those addresses private or transparent (though I
understand that it's not the issue in your case here), then that's
another reason to agree with Stut's point about shying away from BCC.
--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107
Give a man a fish, he'll eat for a day. Then you'll find out he was
allergic and is hospitalized. See? No good deed goes unpunished....
--- End Message ---
--- Begin Message ---
The other possible question you might want to ask is:
How will the message be formatted and sent... some services such as
hotmail will flag the message as spam based on the multiple mail
deliveries when used option #2. And there are also many other
considerations related to spam.
Instead what you could consider doing:
Option #3)
Write a subroutine that would group recipients based on host, and send
one mail to each host with bcc. Also I would not use the internal mail
function, instead I would try a full smtp class, especially when using
mime that has built in support for services such as hotmail, yahoo, etc.
Whenever we send email list to hotmail users I format the message
differently then when we send to yahoo, and so on .... I would avoid
allowing sendmail to handle the function of grouping ...
Hope it helps...
Marek
Philip Thompson wrote:
On 10/7/07, Stut <[EMAIL PROTECTED]> wrote:
Martin Zvarík wrote:
Hello--
I want to send email to 100+ recipients. Two choices I thought of:
1) once call mail() and use BCC, but the negative of this method is that
every recipient in BCC has header "To" same (so I used to put my email
in here - not email of one of the recipients).
2) several calls to mail() function with each recipient's emal
Why are you sending mail to 100+ recipients? Is it something you do
often? Is it always the same recipients? When are you sending them?
If it's a mailing list use mailing list software as Daevid suggested -
it's what it's for!
I would recommend avoiding the use of BCC from PHP.
-Stut
Why avoid Bcc from PHP?
~Philip
--- End Message ---
--- Begin Message ---
So, my question is: are there any good practices that I should be aware
of or example websites I can study, that will give me the knowledge I
need? I'm not looking for references on general software design
approaches, but practical information on how to apply them to PHP websites.
You could do worse than looking at this:
http://www.phpguru.org/static/ApplicationStructure.html
Lots of people like to over complicate their structure, but this (I
find) usually suffices. Plus it's easy to navigate and interpret.
PS. Smarty has nothing to do with structure - it's got everything to do
with needlessly reducing performance.
--
Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk
Knowledge Base and HelpDesk software
that can cut the cost of online support
--- End Message ---
--- Begin Message ---
On 10/7/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
> > it just so happens i
> > have a good friend with a very strong php background who tells me
> > practically the same thing, ppp isnt that big of a deal.
>
> It isn't.
i disagree.
> i disagree, why,
> > is it because ive had a classical education in oop
>
> What the heck is a classical education in OOP? Are you saying your old
> and therefore smarter? Young and therefor smarter? WTF?
just saying i studied oop in college. i started w/ c++; moved on to java
and then took a brief look
at .net and delphi (very brief :)) anyway coming from those languages to
php4,
well php4 looks minimalistic.
age has nothing to do with it; i provided 4 definitions from 4 books
on oop; they all conicide w/ what ive been saying about encapsualtion;
infact they practically
say encapsulation is data hiding.
i would like to see an example of encapsulation where data hiding isnt
required to conceal the
implementation.
> , because ive worked with
> > a number of languages that dont allow you to create class members
> without
> > specifying an access level?
>
> Many of us have done this. Your point?
php4 is the first language i encountered that didnt offer ppp.
> for those reasons and because ive had the
> > misfortune of working in places that have tightly coupled code. im
> talking
> > about hundreds of thousands of lines of madness. ppp could have saved
> these
> > systems greatly.
>
> Once again, just because someone writes bad programs doesn't make PPP
> the superior choice. A shitty programmer faced with PPP will more than
> likely declare all of their member variables as public and as such will
> have gained nothing.
>
> > Also, don't forget that abstraction, encapsulation, and information
> > > hiding all have a price
> >
> > if youre referring to performance i think the price of not using these
> tools
> > is also worth mention. namely code which is easily subject to tight
> > coupling, which as i said leads to systems that are difficult to
> maintain
> > and extend.
> > personally i value maintainability and extensibility over performance,
> but
> > thats merely a personal preference.
>
> Tell that to an embedded systems programmer.
ok.
embedded systems programmer - i develop web applications w/ php. i use oop
and build systems designed for large audiences and large numbers of
developers.
being able to extend, modify, and maintain the code are more important than
small
performance gains here and there; because i can always call dell for another
1u.
> (tony)
> > > I strongly disagree. It *IS* possible to write perfectly adequate OO
> > programs
> > > using PHP 4. If you cannot then you have been taught some bad habits.
>
> Hear hear.
on the contrary, im trying to emphasize what i have learned to be bad habits
so others
might not use them in the first place. one of which i have mentioned
is allowing direct access to class members. and i was never taught to do
that, because as
i mentioned, i worked with languages that require use of ppp in the past,
and one of the first
things thats covered is marking member variables private, and certain member
functions of
course.
> and what exactly does adequate mean? any oo php4 'program' is inherently
> > weak for the reasons i have sighted, namely the implementation can be
>
> I'm sur eyou meant "cited" above.
yes
> latched onto producing tightly coupled code.
> > dont worry tony, i can construe some pretty decent php4 code myself; i
> wrote
> > a date time package that ive ported to 3 projects including a conversion
> to
> > php5 in one of those. the point is that the other developers i work
> with
> > dont have a clue about object oriented concepts which in my experience
> > constitutes the vast majority of php developers. the even bigger point,
> on
> > the topic of this thread is that php4 is out the door, so there is yet
> > another possibly more important reason not to waste time learning oop
> > studying php4.
>
> Once again, if you suck at programming you suck at programming. Those
> sucky programmers are probably going to declare all their member vars
> public, aren't going to understand encapsulation, probably will have
> terrible class hierarchies, etc. You can't make a good programmer by
> holding their hand.
again; all the books ive ever read on oop, show class member variables being
marked private.
also, they emphasize protecting certain methods as well. php4 doesnt offer
ppp, so it opens
the door to bad habits.
you can however protect your underlying codebase from misuse. how practical
is it
to get a company that consists entirely of excellent programmers?
> ive studied oop for years and worked with a number of oop languages; many
> of
> > the bad habits i had at one point or another have been removed. guess
> what
> > the first one was, not letting client code access member variables
> directly
> > :)
>
> I've worked with many languages too. I found my code getting better
> while using C. I learned to properly prefix function names, collect them
> together in the same file, use structures instead of 20 parameter
> functions, etc. Lessons are learned wherever you spend your time.
>
> > if you dont mind brittle oop code, php4 will suffice. if you want to
> tap
> > into real object oriented features, many of which i consider fundamental
> > (such as ppp [to name just one]) go for php5. truthfully i still think
> > there are some features missing; interface hierarchies being the main
> one,
> > perhaps well get lucky in php6...
>
> Nothing brittle at all about PHP4 code. I can completely screw up any
> PHP5 code you send my way too.
>
ok;
without modifying this class, set the value of $someInt to a value greater
than
100 or less than 0.
class TryToViolateEncapsulation {
private $someInt = 0;
public function getSomeInt() {
return $this->someInt;
}
public function setSomeInt($someInt) {
$success = false; // assume failure
$someInt = (int) $someInt;
if($this->isValid($someInt)) {
$this->someInt = $someInt;
$success = true;
}
return $success;
}
private function isValid($someInt) {
$isValid = true;
if($someInt < 0 || $someInt > 100) {
$isValid = false;
}
return $isValid;
}
}
-nathan
--- End Message ---
--- Begin Message ---
On 10/6/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>
> Encapsulation and information hiding are separate topics. It is possible
> to
> have one without the other.
see the revision of my php4 critique.
> here are excerpts from 4
> > books, 2 on java, one on c++, and one on php:
> >
> > This abstraction works only if we are careful to respect its boundaries.
> > An
> > object should be self-governing, which means
> > that the variables contained in an object should be modified only within
> > the
> > object. Only the methods within an object should
> > have access to the variables in that object.
>
> The principle of encapsulation has no such rule! Encapsulation is merely
> the
> act of placing variables and the methods which act upon those variables in
> the same object. It is not necessary to hide any variables.
>
> Encapsulation is NOT data hiding. Take a look at
>
> http://www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html
> http://www.itmweb.com/essay550.htm
> http://c2.com/cgi/wiki?EncapsulationIsNotInformationHiding
>
> > For example, the methods of
> > the Coin class should be solely responsible for changing
> > the value of the face variable. We should make it difficult, if not
> > impossible, for code outside of a class to "reach in" and change the
> > value of a variable that is declared inside the class.
> > Page 220. - 221
> > Java Software Solutions foundations of program design 3rd edition Lewis
> &
> > Loftus
> > The specification of what a function does and how it is invoked defines
> > its
> > interface. By hiding a module implementation, or encapsulating
> > the module, we can make changes to it without changing the main
> function,
> > as
> > long as the interface remains the same. For example, you
> > might rewrite the body of a function using a more efficient algorithm.
> > Encpasulation: Hiding a module implementation in a separate block with a
> > formally specified interface.
>
> The implementation is the CODE behind the method, not the DATA that the
> code
> manipulates. Encapsulation is IMPLEMENTATION hiding, not DATA hiding.
when variables store data needed to realize the outcome of some method;
they
become part of the implementation and therefore should not be exposed to
client code.
> Page 354
> > Programming and Problem Solving with C++ Second Edition
> > Nell Dale, Chip Weems, Mark Headington
> >
> > OOP revolves around the concept of grouping code and data together in
> > logical units called classes. This process is usually referred to as
> > encapsulation,
>
> Correct.
>
> > or information hiding,
>
> INCORRECT
>
> > since its goal is that of dividing an
> > application into separate entities whose internal components can
> > change without altering their external interfaces.
>
> No. The idea behind encapsulation is that the implementation, the code
> behind a method, can change at any time without the outside world being
> aware of it. Data is *NOT* the implemetation.
>
> > Page 113.
> > Zend PHP5 Certification Study Guide
> > Davey Shafik
> >
> > Access control is often referred to as implementation hiding.
>
> But wrongly! Access control is NOT implementation hiding, it is a totally
> separate issue.
>
> > Wrapping data
> > and methods within classes in combination with implementation
> > hiding is often called encapsulation4*
>
> That is correct. But only the implementation (the code behnd each method)
> is
> hidden, not the data.
>
> > The result is a data type with
> > characteristics and behaviors.
> > * However, people often refer to implementation hiding alone as
> > encapsulation.
> > Page 231
> > Thinking in Java Third Edition
> > Bruce Eckel
> >
> >> tonight when i get home ill post
> >> > a snippet from an entry level oop book (such that i recommend
> earlier)
> >> > that
> >> > says almost
> >> > verbatim what i have.
> >> >
> >> > practically any non-trivial class will have member variables that it
> >> uses
> >> > in
> >> > order to facilitate
> >> > its member functions.
> >>
> >> So what? Those variables do not have to be private or protected in
> order
> >> to
> >> function.
> >
> >
> > They dont have to be marked private or protected to function, but in
> order
> > to prevent
> > client code from latching onto the implementation details of the class;
> > they
> > need to be
> > hidden.
>
> Wrong! DATA does not define the implementation, it is the CODE which
> manipulates that data which defines the implementation. The idea of
> encapsulation is that you can change the code within any method at will
> without the outside world being aware of it.
it is also about client code not being able to view and manipulate the
implementation.
if client code can access data that the implementation (as you define it
[not consisting of data])
uses, or relys upon, then the outcome of the implementation can be altered
by client code.
that is a violation of encapsulation, which is precisely why data is part of
the implementation,
and it needs to be concealed from client code to maintain the integrity of
the implementation.
The fundamental concepts of OOP consist of nothing more than encapsulation,
> inheritance and polymorphism. Data hiding, interfaces and all that other
> fancy stuff are irrelevant.
youre missing out on like the last decade or so of oop.
> im not going to take the
> > time to try and explain the power of the interface construct. imho, you
> > have a long way to
> > go before you can grasp the power of the mechanism.
>
> I have never seen any gain from using interfaces in PHP, only pain,
> therefore I refuse to use them. Yet what functinality I am losing? None
> whatsoever.
not every system will benefit from interfaces.
it sounds like you dont fully understand the use of them; and i wont say its
immediately obvious. as i said they offer an additional polymorphic
mechanism
one that php4 doesnt supply.
if you want a great example of how they are used and when extension is not
desirable (because the behavior is passed along) read the first chapter of
heads
first design patterns.
> in summary, i still maintain, based upon the contents of this post and the
> > many previous posts
> > that sitting down and trying to learn oop with php4 is a waste of time.
>
> I strongly disagee. It *IS* possible to write perfectly adequate OO
> programs
> using PHP 4. If you cannot then you have been taught some bad habits.
actually, im trying to illustrate bad habits so they may be avoided by
other developers.
That is just your opinion. My opinion is totally different.
thats why i gave an alternate critique based upon your definition of
encapsulation.
also, i should mention its occurred to me that the abstract class mechanism
in php5 is superior
to your mechanism in php4.
if a method marked as abstract in php5 is not overridden by client code, an
error is raised at
compile time, and the class is not allowed to be utilized.
btw, interface hierarchies are like class hierarchies; one interface can
extend another.
-nathan
--- End Message ---
--- Begin Message ---
On 10/7/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
> On Sun, 2007-10-07 at 11:42 +0100, Tony Marston wrote:
> >
> > The definition of OOP is "programming which is oriented around objects,
> thus
> > taking advantage of Encapsulation, Polymorphism, and Inheritance to
> increase
> > code reuse and decrease code maintenance". It is possible to do this in
> PHP
> > 4 without all the fancy add-ons which appeared in PHP 5. How do I know?
> > Because I have written an entire framework using objects in PHP 4, and
> the
> > result is high code reuse and low maintainence. There are no additional
> OO
> > features in PHP 5 which could deliver any measurable improvements.
not all systems will stand to gain from the features in php5. but ppp is a
very important feature;
i would say any code that is not using ppp could stand to benefit from it.
the interface construct is in my opinion the biggest enhancement to the php5
oop feature set, as
it offers a new avenue for polymorphic code.
>
> > > any oo php4 'program' is inherently
> > > weak for the reasons i have sighted, namely the implementation can be
> > > latched onto producing tightly coupled code.
> >
> > Coupling is the degree of interaction between two modules. Low levels of
> > coupling tend to create code which is more reusable, whereas high levels
> of
> > coupling tend to create code which is less reusable. Coupling is
> therefore
> > directly related to reusability. Making all variables private does not
> *in
> > itself* make the code more reusable, therefore it does not make the code
> > more "coupled" or less "coupled".
all i said is that it prevents client code from becoming dependent upon
implementation
details, which doesnt reduce reusability, but does make the code difficult
to change
because at that point the client code needs to be changed as well.
Well, Nathan is assuming that if you have a public member that you
> probably didn't provide get/set wrappers for it. As such directly using
> the member variable does indeed increase the couple of your code to that
> module because if the variable needs to change in some way it's not
> possible to change the behaviour across the board without updating all
> uses of the member variable.
>
> However, just because it's public doesn't mean it doesn't have a get/set
> wrapper and doesn't mean it should be used directly. Yes, using a
> private declaration enforces use of the get/set wrapper, but once again,
> documentation can make this point also and as such marking a member
> variable as private is just syntactic sugar.
>
> Strangely enough though, PHP5 provides the means to NOT provide get/set
> methods and still modify the behaviour of direct access to a member
> variable via the magic __get/__set methods. Therefore the coupling issue
> is pretty moot for PHP5 regardless of how the variable is accessed.
a class writer would have to use the __get and __ set methods in their class
to expose access to private members. those methods are suitable for special
purposes and anyway client code cant use them if theyre not defined in a
class,
so i dont consider the coupling issue moot.
-nathan
--- End Message ---
--- Begin Message ---
wow, this thread has blown into a massive debate about oop.
well, i guess im the only one on the list who doesnt think php4 brings
enough to the table with its oop offerings.
the conversation has taken focus on what encapsulation is or isnt, but
recall, i pointed out there are many advantages php5 has. i just think ppp
is one of the major features, if not the major feature that php4 lacks that
makes me shy away from it.
im not the only one who feels this way; i just must be the only one on the
list
who cares to say anything about it. well here is an excerpt from the source
of code igniter, a popular open source php framework written in both php4
and
php5.
/**
* CI_BASE - For PHP 4
*
* This file is used only when CodeIgniter is being run under PHP 4.
*
* In order to allow CI to work under PHP 4 we had to make the Loader class
* the parent of the Controller Base class. It's the only way we can
* enable functions like $this->load->library('email') to instantiate
* classes that can then be used within controllers as $this->email->send()
*
* PHP 4 also has trouble referencing the CI super object within application
* constructors since objects do not exist until the class is fully
* instantiated. Basically PHP 4 sucks...
*
* Since PHP 5 doesn't suffer from this problem so we load one of
* two files based on the version of PHP being run.
*
* @package CodeIgniter
* @subpackage codeigniter
* @category front-controller
* @author Rick Ellis
* @link http://www.codeigniter.com/user_guide/
*/
-nathan
--- End Message ---
--- Begin Message ---
""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On 10/7/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
>>
>> > it just so happens i
>> > have a good friend with a very strong php background who tells me
>> > practically the same thing, ppp isnt that big of a deal.
>>
>> It isn't.
>
> i disagree.
And I disagree with you. Marking variables, or even methods, as anything
other than public is purely OPTIONAL. If it were a requirement then the
"public" keyword simply would not exist.
>> i disagree, why,
>> > is it because ive had a classical education in oop
>>
>> What the heck is a classical education in OOP? Are you saying your old
>> and therefore smarter? Young and therefor smarter? WTF?
>
> just saying i studied oop in college. i started w/ c++; moved on to java
> and then took a brief look
> at .net and delphi (very brief :)) anyway coming from those languages to
> php4,
Just because it is what you were taught does not necessarily make it the
gospel truth. Different teachers teach different things, and they cannot all
be right.
> well php4 looks minimalistic.
> age has nothing to do with it; i provided 4 definitions from 4 books
> on oop; they all conicide w/ what ive been saying about encapsualtion;
> infact they practically
> say encapsulation is data hiding.
> i would like to see an example of encapsulation where data hiding isnt
> required to conceal the implementation.
Encapsulation is NOT data hiding. Take a look at
http://www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html
http://www.itmweb.com/essay550.htm
http://c2.com/cgi/wiki?EncapsulationIsNotInformationHiding
Encapsulation is about *implementation* hiding and implementation is *code*,
not *data*. Data is information, and encapsulation does NOT mean
*information hiding.
>>> because ive worked with
>>> a number of languages that dont allow you to create class members
>>> without specifying an access level?
>>
>> Many of us have done this. Your point?
>
> php4 is the first language i encountered that didnt offer ppp.
So what? PHP is the first language I have use in the past 30 years which
supported OOP. None of the languages I have *ever* used have supported the
notion of private or protected access - apart from the permissions system in
the underlying database, of course.
I have learned to program without PPP, so I do not see any signifcant
advantage in using it.
>>> for those reasons and because ive had the
>>> misfortune of working in places that have tightly coupled code. im
>>> talking about hundreds of thousands of lines of madness. ppp could have
>>> saved these systems greatly.
>>
>> Once again, just because someone writes bad programs doesn't make PPP
>> the superior choice. A shitty programmer faced with PPP will more than
>> likely declare all of their member variables as public and as such will
>> have gained nothing.
>>
<snip>
>> (tony)
>>>> I strongly disagree. It *IS* possible to write perfectly adequate OO
>>>> programs using PHP 4. If you cannot then you have been taught some bad
>>>> habits.
>>
>> Hear hear.
>
> on the contrary, im trying to emphasize what i have learned to be bad
> habits
> so others
> might not use them in the first place. one of which i have mentioned
> is allowing direct access to class members. and i was never taught to do
> that, because as
> i mentioned, i worked with languages that require use of ppp in the past,
> and one of the first
> things thats covered is marking member variables private, and certain
> member
> functions of course.
Just because YOU were taught to always make variables private does not mean
that everybody else should do the same. A competent programmer can produce
perfectly functional programs without using ppp at all, so it is not
necessary.
<snip>
> again; all the books ive ever read on oop, show class member variables
> being
> marked private.
So what? None of the books or articles I have ever read on OOP have shown
any such thing. They simply point out where it MAY be used to stop an
incompetent programmer from screwing things up, but whatever obstacles you
put in their way will only cause them to find other ways to screw up.
> also, they emphasize protecting certain methods as well. php4 doesnt
> offer
> ppp, so it opens
> the door to bad habits.
In your opinion.
> you can however protect your underlying codebase from misuse. how
> practical
> is it to get a company that consists entirely of excellent programmers?
It is simply not possible to protect ANY program from misuse. As soon as
someone thinks that they have made something idiot proof the universe
esponds by creating a better class of idiot.
>> ive studied oop for years and worked with a number of oop languages; many
>> of
>> > the bad habits i had at one point or another have been removed. guess
>> what
>> > the first one was, not letting client code access member variables
>> directly
Then how come these languages still offer PUBLIC access as an option?
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
--- End Message ---
--- Begin Message ---
On 10/9/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>
>
> Then how come these languages still offer PUBLIC access as an option?
to expose a well defined interface of course.
-nathan
--- End Message ---
--- Begin Message ---
lame... you can use javascript (which is faster) for this kind of stuff
Mark napsal(a):
Hey,
I've made a nice video where you see ajax in combination with php. and
it works really well although really is misspelled "realy".
Here is the video:
http://magedb.mageprojects.com/videos/MageDB%202nd%20WIP%20demonstration%20with%20AJAX.mpeg
Cool huh?
Mark.
--- End Message ---