문제

i'm rewriting my problem correctly : i have some files and i want in my project one file controle all this files , and then i include only this file in the index and after using the functions of class every function has here self file and codes


<?php

interface Kernel_files_func {
    public function cfg_Adm_kernel();
    public function cfg_aspect();   
    public function cfg_class($class);  
    public function cfg_files();    
    public function cfg_function();     
    public function cfg_mysql($db,$host,$user,$PsUser,$t);          
    }
final class kernel implements Kernel_files_func{    
        public function cfg_Adm_kernel(){
            include_once("cfg_Adm_kernel.php");
                #return new c();
            }
        public function cfg_aspect(){
            include_once("cfg_Adm_kernel.php");

            }   
        public function cfg_class($class){
            include_once("cfg_class.php");
                return new $class();
            }   
        public function cfg_files(){
            include_once("cfg_files.php");

            }   
        public function cfg_function(){
            include_once ("cfg_function.php");

            }       
        public function cfg_mysql($db,$host,$user,$PsUser,$t){
            include_once("cfg_mysql.php");
        $this->DBS =  new  DBmSQL($db,$host,$user,$PsUser,$t);
                 return $this->DBS;
            }
        public function new_table($type,$name,$array){
                $this->DBS->create($type,$name,$array);
            }           
    }

            ?>

now in the other file like cfg_class.php

     <?PHP


       class Home {
       function test(){
        echo "Seccesse";
       }    
    }
      class membership {

    }
             //....     
             ?>

Now in my index.php :

<?PHP
     include ("cfg_system.php");
     $system = new kernel(); 
          $system->cfg_class("Home")->test();
      // after this i can call the other class of cfg_class.php without
       // using  $system->cfg_class("class name")->func name();
      ?>

i want my kernel class controle all the codes , i want to make only my kernel class can open my class and ... i know i just have to use the $system->cfg_class... without using it like $test = new .... ; but i want to make my kernel class like a real kernel for my project ....

도움이 되었습니까?

해결책 2

As I see, you are trying to include a function inside a class declaration. It could be done using usual inheritence using the "implements" keyword... But if we stick to your case, I'd suggest you put the code of the function in the included file, instead of the function declaration, that way you can declare it the way you want in different files like so:

File func.php

<?php
    echo "HELLO WORLD";
?>

File inc.php

<?php 
class _includes_ { 
    function inc_test(){
        include_once("func.php");
    }
}
?>

File test.php

<?php
include"inc.php";
$inc = new _includes_();
$inc->inc_test();  
# after this line, the function won't work as it has not been defined...
# any more if i use it  .... 
test(); // like here ... 
function test(){
    include("func.php");
}
//Now that I declared the function test(), I can use it...
test();
?>

Pay close attention to include and include_once though... include_once means the file can only be included one time per execution, that is why I used include instead of include_once in the above example.

다른 팁

I think there is no solution to this problem. This looks like a bad workaround instead of a real problem. As @N.B. said, this looks like a classic case of the XY problem.

What you're actually asking for, is to 'redefine' a function. PHP hasn't got a method for that (See Redefining PHP function?), and there is very good reason for that: if test() (in func.php) isn't the same as test() (second instance), (at least) one of the functions shouldn't be called test(). Renaming the function(s) is a good idea.

Please share your real problem instead.

It seems that you need to re-factor your solution. I suggest you to go for something more standard like the following:

/* func.class.php */
class MyFunc {
    public test () {
        ...
    }
}

/* sample usage */
include 'func.class.php';
$func = new MyFunc();
$func->test();

/* if you don't need it anymore */
$func = null;

/* to avoid errors */
if ( 'MyClass' === get_class($func) ) {
    $func->test();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top