Hi Ent,
Thanks very much for the detailed reply.
In terms of my remarks that follow, please keep in mind that as far as OO PHP is concerned, I am a newbie and am learning.
I've written a short program to demonstrate my confusion (please see below along with its output). In spite of your explanation, what the results from my experiment tell me is that "static" is superfluous and has no impact whatsoever. With or without the "static" qualifier, if a function is "public", it can be called with or without instantiation of the class. Furthermore I can find no alternative keyword relative to "static".
In the case of visibility, a concept which makes sense to me, is that there are 3 keywords: public, protected, and private and, in PHP, not specifying visibility defaults to "protected". (In the case of "static", if it is not specified, what does it default to?)
What I find odd is that in PHP all public methods seem to be callable with or without instantiation of the class. I'm sure that I am missing something and perhaps you or someone else could point out my misconception.
Code:
<?php
class talk
{
// Since visibility is not specified, I assume this is public. Is that correct?
function Hi() {
echo "Hi from unqualified function Hi(). Am I static or not?<br />";
}
// Since visibility is not specified, I assume this is also public. Is that correct?
static function staticHi() {
echo "Hi from static function staticHi().<br />";
}
protected function protectedHi() {
echo "Hi from unqualified function protectedHi(). I am protected but am I static or not?<br />";
}
protected static function protectedStaticHi() {
echo "Hi from protected static function protectedStaticHi().<br />";
}
}
// No visibility specified (default is "public").
echo "1. Calling unqualified function Hi() without instantiating class talk:<br />";
talk::Hi();
echo "<br /><br />";
echo "2. Calling static function staticHi() without instantiating class talk:<br />";
talk::staticHi();
echo "<br /><br />";
echo "3. Calling unqualified function Hi() after instantiating class talk:<br />";
$talkObj1 = new talk();
$talkObj1->Hi();
echo "<br /><br />";
echo "4. Calling static function staticHi() after instantiating class talk:<br />";
$talkObj2 = new talk();
$talkObj2->staticHi();
echo "<br /><br />";
// With visibility specified as "protected" -- all of these raise errors
/*
echo "5. Calling unqualified function protectedHi() without instantiating class talk:<br />";
talk::protectedHi();
echo "<br /><br />";
Raises error: PHP Fatal error: Call to protected method talk::protectedHi() from context '' in staticFctTest.php
*/
/*
echo "6. Calling static function protectedStaticHi() without instantiating class talk:<br />";
talk::protectedStaticHi();
echo "<br /><br />";
Raises error: PHP Fatal error: Call to protected method talk::protectedStaticHi() from context '' in staticFctTest.php
*/
/*
echo "7. Calling unqualified function protectedHi() after instantiating class talk:<br />";
$talkObj3 = new talk();
$talkObj3->protectedHi();
echo "<br /><br />";
Raises error: PHP Fatal error: Call to protected method talk::protectedHi() from context '' in staticFctTest.php
*/
/*
echo "8. Calling static function protectedStaticHi() after instantiating class talk:<br />";
$talkObj4 = new talk();
$talkObj4->protectedStaticHi();
echo "<br /><br />";
Raises error PHP Fatal error: Call to protected method talk::protectedStaticHi() from context '' in staticFctTest.php
*/
?>
OUTPUT -- of the code above including case 8 but not showing 5, 6, and 7 which produce the same result as 8:
1. Calling unqualified function Hi() without instantiating class talk:
Hi from unqualified function Hi(). Am I static or not?
2. Calling static function staticHi() without instantiating class talk:
Hi from static function staticHi().
3. Calling unqualified function Hi() after instantiating class talk:
Hi from unqualified function Hi(). Am I static or not?
4. Calling static function staticHi() after instantiating class talk:
Hi from static function staticHi().
8. Calling static function protectedStaticHi() after instantiating class talk:
(HERE THE ERROR GETS RAISED IN THE APACE ERROR LOG)
END OF OUTPUT.
Thanks for your help.
Andynic