PHP/Wordpress, Childthemes and functions already defined - php

I'm wondering (I have full access to the serve in case it's a php.ini setting) if there's anyway to "disable parsing of functions if a function was already defined" instead of throwing an error/notice about it?
For example /www/main/deep/file/file.php has:
function homepage_filter_get_map () {
// example1
$generic_filter_array = td_generic_filter_array::get_array();
}
and in /www/main-child/custom.php has, which is called/included/parsed before the file above:
function homepage_filter_get_map () {
//do nothing
}
Essentially, I'm looking for a way to suppress any and all errors outputting about already defined functions while silently ignoring functions that might have the same exact name, but already parsed/defined.
My problem is that the theme I'm using doesn't have full support for Wordpress child themes, just loop files mainly. I know I can just tweak the original files but I want the ability to be able to keep the theme updated without erasing all of our custom tweaks every-time.
Note: Yes, I know you can conditionally call functions, but again I'm looking for a way to do this without editing any of the "main" files since any tweaks done to those get overwritten when updating the parent theme.

if(!(function_exists('homepage_filter_get_map'))){
function homepage_filter_get_map(){
//do code
}
}
This checks if the function 'homepage_filter_get_map' exists. If it doesn't, create the function.

Related

How to override a PHP function?

UberCart for Drupal has some difficulties with currencies. However, by overriding "uc_currency_format", you can at least do some background calculation to give you a good estimate of the converted value. However, as it's part of UberCart Core, you can't edit the file, So you risk losing your code after every update. Also, this function does not have a hook!
That means the only that I can think of dealing with this, is having a module that overrides the function. So my question is...
Is there a way to override an existing PHP function? For example, I have:
function uc_currency_format($value, $sign = TRUE, $thou = TRUE, $dec = NULL)
{
// dont do this
}
But when this gets called, I want it to instead execute this
function uc_currency_format_rewrite($value, $sign = TRUE, $thou = TRUE, $dec = NULL)
{
// do this
}
Is that possible?
It seems to be one of those very rare times you need to hack the core code.
When it comes to this, I try to limit the impact at minimum like this:
Rename original function. In your case, you would go with something like 'uc_currency_format_ORIGINAL'
In your custom module, rename your 'uc_currency_format_rewrite' into 'uc_currency_format'
This way, you will have your own code running.
At next update, you will see in your testing environment (always better to test, before applying updates to production sites) a duplicate function name fatal error. If your hook has not been implemented yet, you will rename the original fuction, again.
This method is not defined in the best practice, of course. Use it at your own risk.
No it is not possible to override a function in PHP. Drupal 7 does not use Zend (rename_function(), override_function()) or OOP in modules. So you could only ask the maintainer for a new hook.
Maybe you could write a patch, which provides this hook and ask the maintainer for implementing it.

unrequire a file once it's been required in PHP

Suppose I do
require('lol.php');
whereby lol.php contains the following function declaration
function lolfunc(){
}
is it possible to "unrequire" lol.php such that I can then require another file
require('lol2.php');
whereby lol2.php contains a function with the same name previously declared in lol.php:
function lolfunc(){
echo "this is lol2 biyotch";
}
and have lolfunc() be the one declared in lol2.php? eg if I call lolfunc() it'll echo "this is lol2 biyotch"??
My answer: don't do that.
Try to work with the original author to include the functions you need into a patched version of the old code and then use that patched version everywhere.
If you can't do that, find out how big a job it would actually be to update all the code to new version. Start with white-box analysis: see what's changed in terms of interfaces, data structures et al. Then examine the calling code to see whether the caller cares about any of the things that have changed.
If you can't even do that, use namespacing or some other form of wrapping so that you can include both libs. However, make sure any initialisaton or setup is done on both libs!
What you should do, is to include the required (sets) of files based on conditional clauses, instead of trying to "unrequire".
So:
if($flag_use_oldver)
{
include("oldver.php");
}
else
{
include("newver.php");
}
If you want a more sophisticated solution, of course you could try to have a wrapper class hierarchy that will extend/override as required, but I think that is a bit over-engineering for a pretty straightforward problem statement.

Classload accessing with PHP - is it possible to load the same class from 2 different resources?

I am just wondering, I actually decided to go down a different route.
I create this file A
C:\somefolder\templates\mytemplate\joomlaoverwrites\libraries\joomla\document\html\renderer\head.php
which overwrites a joomla library file:
C:\somefolder\libraries\joomla\document\html\renderer\head.php
I use the overwrite by using
require_once(__DIR__ . '/joomlaoverwrites/libraries/joomla/document/html/renderer/head.php');
in my index.php of my template
This actually works.
What I now want to do is use my file just as a wrapper class, and sneak my changes into it before returning the answer. E.g.:
public function fetchHead($document)
{
$joomlasOriginalHead = callOriginalFunctionFetchHeadFromTheJoomlaImplementation();
//do my changes to joomlas original answer and return that
}
E.g. from that overwrite, I want to call the original file. Is that somehow possible?

PHP Functions from included files don't execute on Web Server

I am in the process of migrating a site from my personal dev server onto Windstream's business hosting server. I've already run into the issue of having developed using PHP 5.4 only to find out that my static functions won't work on WS's 5.1.4 installation. I've since fixed those issues and am not facing one that I can't seem to find any help for on the internet.
All of the static functions I was using have been rewritten as functions outside the class scope. Instead of having
class Product{
...
public static function myFunction(){}
...
}
I now have
function myFunction(){}
class Product{...}
in my included Product.php file.
However, when I try to call myFunction() from my code, nothing happens. I know the nothingness comes from WS's error handling, but the point is, the function isn't working. To verify this, I have taken the following steps:
Inserted the line echo "entered included"; immediately following the <?php in Product.php. This prints "entered included" on the index page, indicating that my include is working. I have done the same thing before the final ?> with the same results, so I don't think it's getting hung up inside the included file.
I have changed myFunction() in the included file to be simply
function myFunction(){echo "myFunction works";}
A call to myFunction() still makes nothing happen.
I have moved myFunction() to the including file (myFunction() now lives in index.php instead of Product.php; index includes Product.php). This time, myFunction() executes without issue.
To my 'hack it til it does what it should' sensibilities, this tells me that the server is having a problem with functions that are declared in files that are included; honestly, though, I have absolutely no clue what's going on, and any help would be appreciated. Their website is currently down, and they were expecting it to only be offline for a day, so I'll try pretty much anything short of sacrificing a fatted calf.
I know I should be posting more specific code, but since this is a customer's actual website, I'm trying to put as little of the actual code out here as is possible. I'm happy to append specific sections of code to this entry as they are requested for clarification.
Thanks in advance for your consideration.
#Rottingham: First, thanks for the 3v4l link. Second, my assumption about static methods in 5.4 vs 5.1.4 came from this line of php.net's page on static members and methods:
"As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static)."
src - http://www.php.net/manual/en/language.oop5.static.php
Since my version and the server version were on different sides of the 5.3 mark mentioned, I incorrectly assumed that this was my problem.
Third, when I get in from my day job, I'll update my code to show errors and update this post if a solution has not yet been found.
Ultimately, my problem isn't with using static methods (since I don't have them anymore) but with using any function that is declared in an included .php file.

PHP Function Scope Failure

I am struggling to understand scope and what's preventing my new code from working (assuming it is a scope issue).
The following function is in a file PATH.'/includes/custom-functions.php' that references a class:
function infusion() {
require_once(PATH.'/classes/infusion.php'); //PATH is defined in WordPress from ~/wp-content/themes/theme/
return new infusion();
}
The class is reliant on PATH.'/api/isdk.php' and connection credentials from another file within /api/ directory. From within PATH .'/includes/custom-functions.php', I have many other functions that call $infusion = infusion(); and work perfectly.
PROBLEM
I have created a new file: PATH.'/includes/report.php' which I need to access $infusion = infusion();but can't get to work by either repeating the function infusion() definition from above; using require_once();; or using include();. All 3 of those options simply kill the rest of the code and I can only come to the conclusion - well, I have no conclusion.
Any help would be greatly appreciated.
I'm assuming the code isn't using namespaces, therefore you aren't permitted to redeclare the infusion function (either by redefining the function, or re-including the class).
Your includes/report.php file should simply have:
require_once PATH.'/includes/custom-functions.php';
// your other code here ...
$infusion = infusion();
It may be the case that other files / classes that you're including in your file are already requiring custom-functions.php along the line, so you may be able to skip that entirely. Also note that the PATH constant should have already been defined somewhere (either directly or via an included file) before you attempt to use it. If you set your error_reporting to include E_ALL, you'll get a notification in your error log if that constant doesn't exist.
If that fails, your error log(s) may provide some additional background on what your issue is.

Categories