Php exit function question - php

I have noticed a behavior in PHP that makes sense, but I am unsure how to get around it.
I have a long script, something like this
<?php
if ( file_exists("custom_version_of_this_file.php") ) {
require_once "custom_version_of_this_file.php";
exit;
}
// a bunch of code etc
function x () {
// does something
}
?>
Interestingly, the function x() will get registered with the script BEFORE the require_once() and exit are called, and therefore, firing the exit; statement does not prevent functions in the page from registering. Therefore, if I have a function x() in the require_once() file, the script will crash.
Because of the scenario I am attempting (which is, use the custom file if it exists instead of the original file, which will likely be nearly identical but slightly different), I would like to have the functions in the original (calling) file NOT get registered so that they may exist in the custom file.
Anyone know how to accomplish this?
Thanks

You could use the function_exists function. http://us.php.net/manual/en/function.function-exists.php
if (!function_exists("x")) {
function x()
{
//function contents
}
}

You can wrap your functions in a class and extend it to add custom versions.
this_file.php
class DefaultScripts {
function x () {
echo "base x";
}
function y() {
echo "base y";
}
}
if(file_exists('custom_version_of_this_file.php'))
require_once('custom_version_of_this_file.php');
else
$scripts = new DefaultScripts();
custom_version_of_this_file.php
class CustomScripts extends DefaultScripts {
function x () {
echo "custom x";
}
}
$scripts = new CustomScripts();
Results if file exists
$scripts->x(); // 'custom x'
$scripts->y(); // 'base y'
and if it doesn't
$scripts->x(); // 'base x'
$scripts->y(); // 'base y'

Related

class_exists when autoload flag is false

So, as part of a project, I was considering building a flagging system. The idea behind this would be a cron job that runs daily to determine whether each of a series of flags still applied to a specific object (and if so, save that flag data for the object).
// code stub
$flags = $this->getFlags();
foreach($flags as $flag)
{
$className = 'Svc_Flags_'.$flag->flag_code;
if(class_exists($className, false)
{
(new $className())->setFlag();
}
}
And right now, in the dummy code for that class, I have a constructor that echos a simple text message, and the function setFlag() that echos a different text message.
<?php class Svc_Flags_Test extends Svc
{
public function __construct()
{
echo 'construct<br/>';
}
public function setFlag()
{
echo 'set flag<br/>';
}
}
Now, this doesn't work. By that, I mean that I am not seeing either echo.
However, if I do this:
// code stub
$flags = $this->getFlags();
foreach($flags as $flag)
{
$className = 'Svc_Flags_'.$flag->flag_code;
(new $className())->setFlag(); // This is the added line of code
if(class_exists($className, false)
{
(new $className())->setFlag();
}
}
I get the constructor echo, and the setFlag() echo TWICE.
Why is this happening? Now, I'm pretty sure I could just wrap part of this in a try/catch block to get past any errors if a class isn't there, but I'm curious as to why it doesn't seem to find the class unless I explicitly call it before the if statement.

Cannot load custom content type nodes with load_node_multiple or load_node

I have a custom content type called "program" that I am trying to load via a drupal module.
The .module file includes a class called Program that has a method called
getAllPrograms() using include_once(drupal_get_path('module', 'progs') . '/progs.php');
When i try and load nodes using either node_load() or node_load_multiple() i get one of two different errors randomly.
either:
Fatal error: Fatal error: Call to undefined function user_access() in /mypath/modules/filter/filter.module on line 1035
or
Error: Call to undefined function token_get_entity_mapping() in /mypath//sites/all/modules/contrib/token/token.tokens.inc, line 767
Note: 99% of times it is the first error, and occasionally i would recieve the token_get_entity error.
The strange thing is, while i have been trying different things to resolve the error I have been able to get both of these functions to work for a period but as soon as i clear the Drupal Cache i get the error again.
What I have tried
Disabling and enabling the user module via the database.
Checking the paths and status are correct for the main modules (system, user, block etc)
using db_select to get a list of node ids and then use node_load() (with a loop) and node_load_multiple() to load the nodes. This is one of the things that started working for a short time until i cleared the cache.
Tested to see if i can call user_access() from my .module file. This does not work and returns the same call to undefined function error.
Here is the code that I have (not progs an anonymized name)
progs.module
include_once(drupal_get_path('module', 'progs') . '/progs.php');
progs.php
if( !class_exists('progs') ):
class progs
{
//a bunch of properties
function __construct()
{
// load partial includes and objects
$this->load_partial_inclues();
//retrieve all programs that are open
$this->open_programs = Program::getAllOpenPrograms();
}
function load_partial_inclues()
{
//includes
include_once(drupal_get_path('module', 'progs') . '/core/objects/program.php');
}
}
function progs()
{
global $progs;
if( !isset($progs) )
{
$progs = new progs();
}
return $progs;
}
// initialize
progs();
endif;
Note: I load the $progs into the global space so i can call it elsewhere in my module.
program.php
if( !class_exists('Program') ):
class Program
{
//a bunch of properties
public static function getAllOpenPrograms()
{
// This is the line that causes all of the issues.
$result = node_load_multiple('',array('type' => 'program'));
dpm($result);
}
Thanks in advance!
Like Mike Vranckx mentioned, if you call progs() directly when you include it in progs.module, Drupal basically hasn't bootstrapped, i.e. hasn't started running fully yet. Suggest you put your progs() in progs_init() or similar so that Drupal will invoke it at the right time.
Here's a proposed way that follows your initial structure quite closely, and below you will see an alternative that better follows Drupal's conventions.
New progs.module
/**
* Implements hook_init().
*/
function progs_init(){
progs();
}
And modify your progs.php
// Why are you doing this check? Are you defining this class elsewhere in your project? If not you can safely ignore this
//if( !class_exists('progs') ):
// Convention is to name classes with Pascal case btw.
class progs
{
//a bunch of properties
function __construct()
{
// load partial includes and objects
$this->load_partial_inclues();
//retrieve all programs that are open
$this->open_programs = Program::getAllOpenPrograms();
}
function load_partial_inclues()
{
//includes
include_once(drupal_get_path('module', 'progs') . '/core/objects/program.php');
}
}
function progs()
{
global $progs;
if( !isset($progs) )
{
$progs = new progs();
}
return $progs;
}
A more Drupal way:
progs.module
/**
* Implements hook_init().
*/
function progs_init(){
global $progs;
// Consider using drupal_static to cache this
if( !isset($progs) )
{
module_load_include('inc', 'progs', 'progs');
$progs = new Progs();
}
}
progs.inc (convention is to use .inc)
class Progs
{
//a bunch of properties
function __construct()
{
// load partial includes and objects
$this->load_partial_inclues();
//retrieve all programs that are open
$this->open_programs = Program::getAllOpenPrograms();
}
function load_partial_inclues()
{
//includes
module_load_include('php', 'progs', 'core/objects/program');
}
}

PHP: how to check if a given file has been included() inside a function

I have a PHP file that can be include'd() in various places inside another page. I want to know whether it has been included inside a function. How can I do this? Thanks.
There's a function called debug_backtrace() that will return the current call stack as an array. It feels like a somewhat ugly solution but it'll probably work for most cases:
$allowedFunctions = array('include', 'include_once', 'require', 'require_once');
foreach (debug_backtrace() as $call) {
// ignore calls to include/require
if (isset($call['function']) && !in_array($call['function'], $allowedFunctions)) {
echo 'File has not been included in the top scope.';
exit;
}
}
You can set a variable in the included file and check for that variable in your functions:
include.php:
$included = true;
anotherfile.php:
function whatever() {
global $included;
if (isset($included)) {
// It has been included.
}
}
whatever();
You can check if the file is in the array returned by get_included_files(). (Note that list elements are full pathnames.) To see if inclusion occurred during a particular function call, check get_included_files before and after the function call.

creating multiple instances of splFileObject

I have a class similar to this
class x {
function __construct($file){
$this->readData = new splFileObject($file);
}
function a (){
//do something with $this->readData;
}
function b(){
//do something with $this->readData;
}
}
$o = new x('example.txt');
echo $o->a(); //this works
echo $o->b(); //this does not work.
it seems if which ever method called first only works, if they are called together only the first method that is called will work. I think the problem is tied to my lack of understand how the new object gets constructed.
The construct is loaded into the instance of the class. And you're instantiating it only once. And accessing twice. Are different actions. If you want to read the file is always taken, should create a method that reads this file, and within all other trigger this method.
I tested your code and it worked normal. I believe it should look at the logs and see if any error appears. If the file does not exist your code will stop.
Find for this error in your apache logs:
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(example.txt): failed to open stream
Answering your comment, this can be a way:
<?php
class x {
private $defaultFile = "example.txt";
private function readDefaultFile(){
$file = $this->defaultFile;
return new splFileObject($file);
}
function a (){
$content = $this->readDefaultFile();
return $content ;
}
function b(){
$content = $this->readDefaultFile();
return $content ;
}
}
$o = new x();
echo $o->a();
echo $o->b();
Both methods will return an object splFile.

Any way to exit outer function from within inner function?

Within PHP, if I have one function that calls another function; is there any way to get the called function to exit out of the caller function without killing the entire script?
For instance, let's say I have some code something like:
<?php
function funcA() {
funcB();
echo 'Hello, we finished funcB';
}
function funcB() {
echo 'This is funcB';
}
?>
<p>This is some text. After this text, I'm going to call funcA.</p>
<p><?php funcA(); ?></p>
<p>This is more text after funcA ran.</p>
Unfortunately, if I find something inside of funcB that makes me want to stop funcA from finishing, I seem to have to exit the entire PHP script. Is there any way around this?
I understand that I could write something into funcA() to check for a result from funcB(), but, in my case, I have no control over the contents of funcA(); I only have control over the contents of funcB().
To make this example a little more concrete; in this particular instance, I am working with WordPress. I am hooking into the get_template_part() function, and trying to stop WordPress from actually requiring/including the file through the locate_template() function that's called after my hook is executed.
Does anyone have any advice?
Throw an exception in funcB that is not handled in funcA
<?php
function funcA() {
try
{
funcB();
echo 'Hello, we finished funcB';
}
catch (Exception $e)
{
//Do something if funcB causes an error, or just swallow the exception
}
}
function funcB() {
echo 'This is funcB';
//if you want to leave funcB and stop funcA doing anything else, just
//do something like:
throw new Exception('Bang!');
}
?>
The only way I see is using exceptions:
function funcA() {
funcB();
echo 'Hello, we finished funcB';
}
function funcB() {
throw new Exception;
echo 'This is funcB';
}
?>
<p>This is some text. After this text, I'm going to call funcA.</p>
<p><?php try { funcA(); } catch (Exception $e) {} ?></p>
<p>This is more text after funcA ran.</p>
Ugly, but it works in PHP5.
Maybe...
It's not an solution, but you could hook another function that gets called when exit() is requested "register_shutdown_function('shutdown');". And to somehow have this get things continue again or complete to your satifaction.
<?php
function shutdown()
{
// This is our shutdown function, in
// here we can do any last operations
// before the script is complete.
echo 'Script executed with success', PHP_EOL;
}
register_shutdown_function('shutdown');
?>

Categories