Here is my code:
single-location.php
<?php
// include start_dates.php
include('start_dates.php'); // contians the start dates and a helper function contains()
$crazy = crazyness_rad(); // breaks the page for some reason
start_dates.php
<?php
// simple test function that returns a string...
function crazyness_rad() {
return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
}
And when I call this function from single-location.php it breaks the page...this works outside of wordpress just fine.
Why if I call a function defined in an include does it break? Thanks for the help with my noob question.
The error I get is:
Fatal error: Call to undefined function crazyness_rad() in C:\inetpub\larockwww\wp-content\themes\larock-academy\single-location.php on line 51
I just moved the function from the start_dates.php into the single-location.php and it works fine:
single-location.php
<?php
// include start_dates.php
include('start_dates.php'); // contains the start dates and a
// Moved into this file
function crazyness_rad() {
return 'WLHLHDFLDHFLDHF KJDHF KJDHF KLJDHF K';
}
$crazy = crazyness_rad(); // works fine IT seems the include isn't working..
seems that file start_dates.php is not in the correct path. Please include with the absolute path of the file . You can also try get_template_part function to include the file.
https://developer.wordpress.org/reference/functions/get_template_part/
Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.
I am using Facebook Ads API to pull data from ads Reporting.
Below is my code :
<?php
use FacebookAds\Object\AdAccount;
$account = new AdAccount('act_xxxx');
$params = array(
'date_preset'=>'last_28_days',
'data_columns'=>"['adgroup_id']",
);
$stats = $account->getReportsStats(null, $params);
foreach($stats as $stat) {
echo "is it inside the foreach loop \n";
echo $stat->impressions;
echo $stat->actions;
}
?>
I get FacebookAds/Object/AdAccount not found. I checked the path and everything looks correct. any idea, what could be the reason for this error. I am not a PHP Expert, so please do correct me, if something is wrong with my code.
<?php
function __autoload($class) {
require_once $class.".php";
}
Save this file as autoload.php in same directory then add below code at start
<?php
require_once('./autoload.php');
Explanation:
In your code you haven't included the file which contains the class FacebookAds\Object\AdAccount. That's why it gives class not found error.
Above code will make sure that all necessary class files are include in code.
ive got an global include/header.php file like:
UPDATE
folder structure :
/
include/
header.php
functions.php
content/
show.php
include/header.php
<?php
require_once('functions.php');
$settings = blaa;
....
?>
include/functions.php
<?php
function hello()
{
echo "hello world";
}
?>
and now a content file like content/show.php
content/show.php
<?php
require_once('../include/header.php');
echo "show page want to say: ";
hello();
?>
and now if i look in the apache error_log call to undefined function in content/show.php on line...
i cannot find out why :-/
Greetings
If you tweak your header.php code to this:
<?php
define('INCDIR', str_replace('\\', '/', dirname(__FILE__)));
require_once(INCDIR . '/functions.php');
It wont matter where you include it from it will constantly find the right path to the include folder
I want to get the name of the file that includes another file from inside the included file.
I know there is the __FILE__ magic constant, but that doesn't help, since it returns the name of the included file, not the including one.
Is there any way to do this? Or is it impossible due to the way PHP is interpreted?
So this question is pretty old, but I was looking for the answer and after leaving here unsatisfied, I came across $_SERVER['SCRIPT_FILENAME']; Of course this works if the php file doing the including is a web page.
This gives you the full path of the "including file" on the server. eg /var/www/index.php. so if you want just the filename, eg index.php, you will need to use basename() eg
basename($_SERVER['SCRIPT_FILENAME']);
So, if in your index.php you have the following line:
<?php include("./somephp.php"); ?>
and in somephp.php you have
echo "this is the file that included me: " . basename($_SERVER['SCRIPT_FILENAME']);
you will get
this is the file that included me: index.php
output to the browser. This also works if the user is accessing your file without explicitly including the filename in the url, eg www.example.com instead of www.example.com/index.php.
Solution
Knowing that the functions used to include files are include, include_once, require and require_once, also knowing that they are reserved words in PHP, meaning that it will not be possible to declare user functions with the same name, and based on wedgwood's idea of using debug_backtrace you can actually work out from what file the include was called.
What we are going to do is iterate over the backtrace untill we find the most recent call to any of the four include functions, and the the file where it was called. The following code demostrate the technique:
function GetIncludingFile()
{
$file = false;
$backtrace = debug_backtrace();
$include_functions = array('include', 'include_once', 'require', 'require_once');
for ($index = 0; $index < count($backtrace); $index++)
{
$function = $backtrace[$index]['function'];
if (in_array($function, $include_functions))
{
$file = $backtrace[$index]['file'];
break;
}
}
return $file;
}
The above code will return the absolute path of the file where the last include happened, if there hasn't been any include it will return false. Note that the file may has been include from a file that was included from another file, the above function only works for the deepest include.
With a simple modification, you can also get the last included file:
function GetIncludedFile()
{
$file = false;
$backtrace = debug_backtrace();
$include_functions = array('include', 'include_once', 'require', 'require_once');
for ($index = 0; $index < count($backtrace); $index++)
{
$function = $backtrace[$index]['function'];
if (in_array($function, $include_functions))
{
$file = $backtrace[$index - 1]['file'];
break;
}
}
return $file;
}
Observations
Note that __FILE__ is not the included file, but the current file. For example:
file A.php:
<?php
function callme()
{
echo __FILE__;
}
?>
file B.php:
<?php
include('A.php');
callme();
?>
file index.php:
<?php
include('B.php');
?>
In the above example, in the context of the file B.php the included file is B.php (and the including file is index.php) but the output of the function callme is the path of A.php because __FILE__ is in A.php.
Also note that $_SERVER['SCRIPT_FILENAME'] will give the the absolute path to the script requested by the client. If $_SERVER['SCRIPT_FILENAME'] == __FILE__ it means that the current file is the requested one, and therefore there probably hasn't been any includes...
The above method checks if the current file is the requested one, but not if it hasn't been included (below is an example of how the requested file can be included). An actual solution to check if there has not been any inclusions could be to check count(get_included_files()) == 1.
The requested file can be an included file in the following way:
file x.php
<?php
$var = 'something';
include('index.php');
?>
file index.php
<?php
if (!isset($var))
{
include('x.php');
exit;
}
echo 'something';
?>
In the above example, the file index.php will include x.php, then x.php will include index.php back, afterwards index.php outputs "something" and returns control to x.php, x.php returns control to index.php and it reaches exit;
This shows that even if index.php was the requested script, it was also included from another script.
I can't find the easy way to cover this.
But If the including one is really important to you, you could hack it with some global variable and your own include function.
e.g.
<?php
$g_including_files = array();
function my_include($file) {
$bt = debug_backtrace();
global $g_including_files;
$g_including_files[basename($file)] = $bt[0]['file'];
return include($file);
}
May that be helpful for you :)
This is actually just a special case of what PHP templating engines do. Consider having this function:
function ScopedInclude($file, $params = array())
{
extract($params);
include $file;
}
Then A.php can include C.php like this:
<?php
// A.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
Additionally, B.php can include C.php the same way without trouble.
<?php
// B.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
C.php can know its includer by looking in the $params array.
<?php
// C.php
echo $includerFile;