-2

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 ....

  • After you use it once, delete the file. [**See here**](http://us3.php.net/manual/en/function.unlink.php) – blex May 05 '14 at 11:55
  • Why can't you just set a variable to `false`, test the variable for a `true` value at the start of your function, then set it to `true` in the function? After the first run the variable will always be `true`, thus the function will exit before running anything else. – Ken Herbert May 05 '14 at 11:57
  • What's the problem? If you don't call ``test()``, it wouldn't be executed. – Stephan Vierkant May 05 '14 at 12:03
  • Why don't you use multiple classes? – Stephan Vierkant May 05 '14 at 12:15
  • 6
    Classic case of XY problem - you're telling us that you have an idea about how something's supposed to work instead of what actual problem you have. In other words - what you want to do here makes 0 sense. The workarounds suggested are simply not fit. Instead, what you should do is approach the question from different angle - tell us what you're doing, what you tried and what you expect. In that case, you will probably get a lead on how to do it properly instead of using hackish approaches. – N.B. May 05 '14 at 12:21

3 Answers3

3

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.

Community
  • 1
  • 1
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • Hi,to explane in my project i'm working to create a file who controle all the other file, in other words when i need file1 i call it using this file , and if i want only file2, I use the same method and use it in my project ..... i'm using class and abstract and implements and some interface ... i want to avoid using many of includes .... – user136389 May 05 '14 at 18:16
1

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.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • ``inc_test()`` has no arguments, so calling ``inc_test("func.php")`` will give an error. – Stephan Vierkant May 05 '14 at 12:14
  • You are right... I simply copied the error from OP. I'll correct it – Salketer May 05 '14 at 12:24
  • I think you've improved a solution that is a wrong solution in the first place. See @N.B.'s comment on the question. – Stephan Vierkant May 05 '14 at 12:28
  • @StephanVierkant you are right, I have simply displayed a "maybe ok" workaround. It is a hack that is used by some of our codes because we want to limit the file sizes that we open in our IDE... In anyways, I'm just trying to display a solution to OP's exact case, although it is based on some design flaws at start. – Salketer May 05 '14 at 12:32
  • Hi,to explane in my project i'm working to create a file who controle all the other file, in other words when i need file1 i call it using this file , and if i want only file2, I use the same method and use it in my project ..... i'm using class and abstract and implements and some interface ... i want to avoid using many of includes .... – user136389 May 05 '14 at 18:00
0

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();
}
Mahdi
  • 9,247
  • 9
  • 53
  • 74