I want to divide the code I have written into smaller manageable files. One file has over 2500 lines and I think it might be better to put some of its code into a separate header file. However, as soon as I separate the code and run it. I get the following error:
Uncaught Error: Class 'Document' not found
Here is the code of my large-file.php:
<?php
require_once("common-head.php");
/* Some more code */
$document = new Document($document_html);
Here is the code of my common-head.php:
<?php
/* Some code */
require_once('vendor/autoload.php');
use DiDom\Document;
/* Some more code */
Both the files are located in the same directory so the path to vendor/autoload.php does not change. However, if the code is placed in separate files as I have shown above, I get the error:
Uncaught Error: Class 'Document' not found
If I take all the code out of common-head.php and place it in my large-file.php in place of require_once("common-head.php");. It works without any error. How can I resolve this issue?
Just using use in same file.
// large-file.php
use DiDom\Document;
require_once("common-head.php");
/* Some more code */
$document = new Document($document_html);
see - PHP namespaces and "use"
Related
I am trying to follow a WordPress tutorial by Imran Sayed - Codeytek Academy (https://www.youtube.com/watch?v=lNtw4yxEydM&list=PLD8nQCAhR3tT3ehpyOpoYeUj3KHDEVK9h) to allow wordpress to build a menu and then inject into the html. I have followed the turorials 22, 23 and 24 (from the playlist) trying to use this in my own project.
I have copied the code and folder/file structure and added in the class, helpers, singletons and autoloaders. BUT everytime I try and run the code i get
Fatal error: Uncaught Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
I have changed the text domain from aquila to infocus as thats whats in my project in all the locations. BUT i am completly stuck and can't work out why my code is not running.
Think this is the code thats causing the problems as it can't find the class 'INFOCUS_THEME\Inc\Menus'
<?php
$menu_class = \INFOCUS_THEME\Inc\Menus::get_instance();
$header_menu_id = $menu_class->get_menu_id( 'infocus-header-menu' );
$header_menus = wp_get_nav_menu_items( $header_menu_id );
?>
I have uploaded it to my github account and post the link here, as I thought that is the best way.
https://github.com/wkey1980/infocus
When declaring a variable as a class you must use 'new' to initiate the class.
$menu_class = new \INFOCUS_THEME\Inc\Menus();
//and then you can do
$menu_instance = $menu_class->get_instance();
I'm a beginner to WordPress and PHP, and I'm trying to add a custom settings options page to my WordPress theme by defining a class that is used to generate the page. When I attempt to create an object in the functions.php file to generate the page, I get an error message stating that the class cannot be found.
I've spent a while searching for solutions and messing with the code, but I couldn't find anything that works. The file definitely exists (I can find it in the specified location in file explorer and open/edit it in my IDE). If I just paste the code from my class file directly into functions.php with the class declaration and constructor removed, everything works as expected.
I'm running XAMPP on Windows.
Error message:
Fatal error: Uncaught Error: Class 'My_Class' not found in C:\xampp\my-path-to-site\my-theme\functions.php
in \my-site\functions.php:
include('/folder/class.my-class.php');
$my_options = new My_Class;
$my_options->__construct();
in \my-site\folder\class.my-class.php:
class My_Class
{
private $options;
function __construct() {
add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
add_action( 'admin_init', array($this, 'option_settings_init'));
}
function option_add_admin_menu( ) {
add_options_page('My Options', 'Options', 'manage_options',
'options', array($this, 'option_options_page');
}
// rest of code that registers settings & fields
}
EDIT: I changed "include():" to "require()" as suggested, but now I am getting two different error messages:
Warning: require(/setup/class.my-class.php): failed to open stream: No such file or directory in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29
Fatal error: require(): Failed opening required '/setup/class.my-class.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\my-site\wordpress\wp-content\themes\my-theme\functions.php on line 29
Effectively, you don't have the right path and include will allow you to continue if the file doesn't exist.
When including or requiring a file, if the path you supply starts with a / or \ then PHP will treat it as a path from the root of the current filesystem. When you supply a path that doesn't start with one of those, PHP thinks it is a relative path it will try to guess which file to include based on where the current file is and other directories it knows about.
To fix you will likely want to do the following:
require_once __DIR__.'/folder/class.my-class.php';
See the docs on include, include_once, and as well as __DIR__.
Recommendation:
Whenever including a file you should try to use require_once whenever possible. If it is a file that you know can be included multiple times then you may use require. If it is a file that is OK to be omitted if it for whatever reason doesn't exist, then you may use include_once. If the file can be both, only then should you use include.
However, as an experienced programmer I can also tell you that if you are using either include_once or include you are doing something wrong and should be checking if a files exists before trying to blindly include it.
Also, I highly recommend having the below code active at all times. This will help you catch breaking errors before they have a chance to actually break. Or at least grant you a better understanding of why something broke.
ini_set('display_errors', '1');
error_reporting(-1);
Please check my comments inside the code
in \my-site\folder\class.my-class.php:
<?php
class My_Class
{
private $options; //if you want receive a option
function __construct($options) { //You need receive this option here
$this->options = $options; //and atribut it here
//add_action( 'admin_menu', array($this, 'option_add_admin_menu'));
//add_action( 'admin_init', array($this, 'option_settings_init'));
}
function option_add_admin_menu() {
//add_options_page('My Options', 'Options', 'manage_options',
//'options', array($this, 'option_options_page');
}
// rest of code that registers settings & fields
}
in \my-site\functions.php:
<?php
include_once('folder/class.my-class.php'); //removed the root bar
//You are waiting for a option in the class, so pass this option
$my_options = new My_Class('some option');
//$my_options->__construct(); //You don't need this here, the constructor is used inside the class.
I'm working on a Drupal 8 custom module that renders an image in a block. The following lines are being flagged by codesniffer:
$advertFile = \Drupal\file\Entity\File::load($advert1);
$advertStyle = \Drupal\image\Entity\ImageStyle::load('ad_banner')->buildUrl($advertFile->getFileUri());
The error being generated is:
Namespaced classes/interfaces/traits should be referenced with use
statements
I've placed the following use statements at the top of the file and altered the lines to File::load... and ImageStyle::load... respectively. This throws errors and the site stops working.
Not quiet sure how to express namespaced classes correctly in a custom D8 module, so any help appreciated!
Update:
The top of the file looks like this:
<?php
namespace Drupal\my_module\Plugin\Block;
/**
* #file
* Contains \Drupal\my_module\Plugin\Block\AdvertBlock.
*/
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\image\Plugin\Field\FieldWidget\ImageWidget;
use Drupal\image\Entity\ImageStyle;
The error message I am getting is: 'The website encountered an unexpected error. Please try again later.'
Thanks
Im using a library called PDFmerger , naturally, it merges pdfs together. I wish to run a cron every night that builds these to have them ready for the next day. The issue is when I run the code after the first one it fails because it is attempting to redeclare the class.
The for statement...
foreach ($pdfs_to_build as $pdf) {
// do stuff
$this->merge_pdfs($pdf['pdf_id']);
}
And my pdf merging code that gets ran in the loop...
$pdf = new PDFMerger;
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-1.pdf', '1');
$pdf->addPDF(DIR_FINAL_PDFS.$pdf_id.'/custom-page-1.pdf','1');
$pdf->addPDF(DIR_BOOKS.$book_path.'/static-pages/page-2.pdf', '1');
$pdf->merge('file', DIR_FINAL_PDFS.$pdf_id.'/final-build-'.$pdf_id.'.pdf');
Error after the first 1 is complete and we've onto the next one...
Fatal error: Cannot redeclare class PDFMerger in /var/www/example/pdfmerger/PDFMerger.php on line 24
Im wondering is there a workaround for this?
The problem isn't that you're calling new PDFMerger twice, that's perfectly acceptable. The problem is that the file:
/var/www/example/pdfmerger/PDFMerger.php
Is being include'd (or otherwise executed) twice, which has on line 24:
class PDFMerger {
That is what is triggering your error, that class has already been declared. Figure out why that file is being included twice and your error will clear up.
Your code is fine. That won't fail. However, not listed in what you posted, you are including PDFMerger.php.
Probably by having the include 'PDFMerger.php'; shown at https://pdfmerger.codeplex.com/ run inside the loop.
Just move that line out of the loop and/or change the include to include_once.
I was upgrading magento when something went wrong and now when I try to login to admin, I am unable to log in to back end admin of magento and I get the following error
Fatal error: Call to undefined method Zend_XmlRpc_Value::getGenerator() in /home/boutique/public_html/app/code/core/Zend/XmlRpc/Request.php on line 413
and the code on respective lines is
/**
* Create XML request
*
* #return string
*/
public function saveXml()
{
$args = $this->_getXmlRpcParams();
$method = $this->getMethod();
$generator = Zend_XmlRpc_Value::getGenerator();
$generator->openElement('methodCall')
->openElement('methodName', $method)
->closeElement('methodName');
I cant understand why this issue is happening, I tried replacing request.php and response.php files from fresh download of magento..
can body help me? why this eror is popping?
There's something about your installation of PHP and Magento that's broken — for some reason the Zend_XmlRpc_Value object that's instantiated doesn't contain a getGenerator method. The class for this object is normally defined in
lib/Zend/XmlRpc/Value.php
However, it's possible there may be a class override in place at
app/code/core/Zend/XmlRpc/Value.php
app/code/community/Zend/XmlRpc/Value.php
app/code/local/Zend/XmlRpc/Value.php
It's also possible your system may have another version of the zend framework installed somewhere in the PHP include path.