Prevent PHP from requiring_once

Debugging another PHP code, I would like to selectively override one of my classes. The class is enabled through:

require_once('classname.php');

But it appears in different places of the application. I would rather "simulate" require_onceso that it never starts at all. That is, just determine class classnamehow I want it. Then, the next time the file was require_once' ed, it will be marked as already loaded and therefore not reloaded.

I can create my own classname.php file, but I would prefer to keep the testing that I am doing, contained in a single file, as I am doing this, perhaps for many classes, and it would be easier for me to control overriding.

In Perl, what I would like to do would be:

$INC{'classname.pm'} = 1;

Is there a way to access the PHP equivalent of Perl %INC?

Update : consistently wondering what PHP is not letting you do ...

My workaround was to use runkit runkit_method_redefine . I load the classes that I was trying to prevent, and then overriding all the methods that I tried to β€œlayout”, for example:

require_once('classname.php');
runkit_method_redefine('classname','method','$params','return "testdata";');
+3
source share
3 answers

A place:

<?php return; ?>

at the very top of the file classname.php.

+2
source

Then, the next time the file was require_once'ed, it will be marked as already loaded and therefore not reloaded.

require_once does this for you.

Alternatively, you can use something hacked as

if(!class_exists("my_class")) {
  class my_class {}  // define your class here
}
0

, , , "required_once", .

, :

1) "" .

2) Go to the class file and include your class and return it before another class is defined.

0
source

Source: https://habr.com/ru/post/1753380/


All Articles