|
Posted by Jochem Maas on 03/16/05 00:01
Colin Ross wrote:
> under PHP5 at least, you should only be able to use $Class::method for
> public methods with a class, correct?
there is a reason the class is called Test. say it slowly. let it
sink in... ;-) basically write a test class and play with it, e.g:
<?php
class Test
{
public static function staticDoPF() { call_user_func( array(self,"privFunc") ); }
public function doPF() { call_user_func( array($this,"privFunc") ); }
private function privFunc() { echo "Yeah PrivFunc!\n"; }
public static function staticDoPSF() { call_user_func( array(self,"privStatFunc") ); }
public function doPSF() { call_user_func( array($this,"privStatFunc") ); }
private static function privStatFunc() { echo "Yeah privStatFunc!\n"; }
}
$t = new Test;
// good
echo "works:\n-----\n";
Test::doPF();
$t->doPF();
call_user_func( array("Test","doPF") );
call_user_func( array($t,"doPF") );
Test::staticDoPF();
$t->staticDoPF();
call_user_func( array($t,"staticDoPF") );
call_user_func( array("Test","staticDoPF") );
Test::doPSF();
$t->doPSF();
call_user_func( array("Test","doPSF") );
call_user_func( array($t,"doPSF") );
Test::staticDoPSF();
$t->staticDoPSF();
call_user_func( array("Test","staticDoPSF") );
call_user_func( array($t,"staticDoPSF") );
// bad
echo "\ndont works | fatal errors (read source):\n-----\n";
//Test::privFunc();
//$t->privFunc();
//call_user_func( array("Test","privFunc") );
//call_user_func( array($t,"privFunc") );
//Test::privStatFunc();
//$t->privStatFunc();
//call_user_func( array("Test","privStatFunc") );
//call_user_func( array($t,"privStatFunc") );
?>
>
> i.e.
>
> class Test
> {
> private function doIt_pri() { echo "done it!\n"; }
> public function doIt_pub() { echo "done it!\n"; }
> function doIt_pub2() { echo "done it!\n"; }
> }
> $t = Test::doIt_pri(); // should fail
> $t = Test::doIt_pub(); // should work
> $t = Test::doIt_pub2(); // should also work since public is the
> default (assumed)
>
Test
> ==
> OR, does that rule only pertain to when a method is private, only
> methods WITHIN the class can call it?
>
Test
>
> On Tue, 15 Mar 2005 17:06:44 +0100, Jochem Maas <jochem@iamjochem.com> wrote:
>
>>André Pletschette wrote:
>>
>>>Hi,
>>>
>>>What do I have to do to call a function from $classname?
>>>
>>>Like: $classname->test( );
>>
>><?php
>>// does $classname contain a name of a class? e.g:
>>
>>class Test
>>{
>> function doIt() { echo "done it!\n"; }
>>}
>>
>>$className = "Test";
>>$t = new $className;
>>$t->doIt();
>>
>>// also look at the functions
>>// call_user_func() and call_user_func_array(),
>>// e.g:
>>
>>call_user_func( array($className,"doIt") );
>>
>>?>
[Back to original message]
|