Finaly I used all this ideas to build
some kind of metaclass
So every class should extend(inherit) metaclass
function "publish()" initialize all function as public
function "privatise()" takes a coma-delimited string with
private functions' names and make'em private.
function "run()" is function which run other function
every function has to use "try()" function firstly,
in order to find out if it is private or public.
array "$this->funcs[]" contains functions' names and their access
rights(public or private).
I've put some comments to explain it a bit more detaily.
Here is some example
In this example 'MetaClass' class is a parent class,
which realise emulation of private and public functions
for his childs.
and 'Example' class is an example of child classes.
<?
class MetaClass{
function MetaClass(){
$this->publish("MetaClass");
}
function publish($class_name){
$class_methods=get_class_methods($class_name);
for($i=0; $i<count($class_methods); $i++)
$this->funcs[$class_methods[$i]]="public";
}
function privatise($str_of_funcs){
if(!strstr($str_of_funcs, ","))
$arr_funcs[0]=trim($str_of_funcs);
else
$arr_funcs=explode(",", $str_of_funcs);
for($i=0; $i<count($arr_funcs); $i++)
$this->funcs[trim($arr_funcs[$i])]="private";//making funcs
//to be private
}
function run($method){
$method_arr=explode("(", $method);
$method_name=$method_arr[0];
$method_name=str_replace("\$this->", "", $method_name);
$access_right=$this->funcs[$method_name];//identify access rights
$this->funcs[$method_name]="public";//make function to be a public
$a=eval($method);//runing function
$this->funcs[$method_name]=$access_right;//restore access rights
return $a;
}
function try($method){
if($this->funcs[$method]=="private"){
print "Fatal error: access denied \"$method()\" is private\r\n";
return false;
}
return true;
}
}
class Example extends MetaClass{
function Example(){//every constructor of MetaClass child
//have to contain these 2 funcs:
$this->publish("Example");//make all methods of the class "Example"
//to be public
$this->privatise("foo");//function foo is going to be private
}
function bar($arg){//public function
if(!$this->try("bar")) return false;//trying if access is set to
//public
$result=$this->run("\$this->foo(2, $arg);");//runing private function foo
print $result;
}
function foo($a, $b){//private function
if(!$this->try("foo")) return false;//trying if access is set to
//public
print $a+$b."=".$a."+".$b."\r\n";
return $a+$b;
}
}
$a=new Example;
print "try to use private foo: ";
$a->foo(5, 6);
print "try to use foo through public bar\r\n";
$a->bar(3);
?>
It works but it doesn't look too good cuz:
1 every function's code has to be started with
if(!$this->try("foo")) return false;
2 every private function have to
be run by "run()" function.
And finaly I gonna try another idea about all this stuff
I gonna try to write some class which will open the file
which he was run in. And check it with the help of regular
expressions if its' code contain any private functions outside
the class definition.... I don't think it's bgetter idea but..
who knows, may it is worth..
:...::lol::...: