wordpress page template requre_once() failed - php

I want to use a class file in a WordPress page template, but after adding the require_once() method, and trying to instance the class, I got
Fatal error: Class 'ClassName' not found,
the class file is displayed as html to the output page.
Any idea why this is happening? (I put the page template file and the class file under the theme directory)

Try: require_once(TEMPLATEPATH . '/ClassName.php');
Edit: Sorry, the file apparently has been found correctly. So then you need the class to be defined in that file looking like this:
class ClassName {
Your code here
}
Do you have included the class {} construct?
Is the name of your class the same as you are trying to instantiate? $obj = new ClassName(); ?

Related

Yii2 -windows 10 xampp path error

here i have a Crypt.php file . its a class have two function, and stored it in "\backend\components" folder. i call this file(class) in my controller using this code
$security = new \backend\components\Crypt();
at run time i am getting this error:
"Unknown Class – yii\base\UnknownClassException
Unable to find 'backend\components\Crypt' in file: E:\xampp\htdocs\pope-Admin/backend/components/Crypt.php. Namespace missing?"
in this path half of them have slash(/) and half of the part have back slash() how to solve it?
In your Crypt class file, include namespace declaration like so:
<?php
namespace backend\components;
class Crypt {
...
}
?>
Use the include(_once) or require(_once) keywords to include the Crypt.php file, then just use new Crypt(). You cant define an instance of a class like that, you have to include the filw containg the class code and only then you can use the new keyword.

How do I make a custom override of Joomlas JHtmlTabs?

I am attempting to make a simple override to the JHtmlTabs function without editing the core files.
I've tried making a copy of the tabs file (found in /library/html/html/tabs.php), and modifying it to simply extend the original file, but any modification I make either results in the page crashing with Fatal error: Class 'JHtmlTabs' not found or my modifications don't appear.
Is there a specific way to handle overriding this?
You need to load the class into memory. The error is the result of your extending a class it can't find. At the top of the class file extending the JHtmlTabs class add this line:
JLoader::register('JHtmlTabs', JPATH_ROOT . '/libraries/joomla/html/tabs.php');
This should load the class into memory and remove your error.

Trouble with Namepaces and Class Loading

Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich
try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();
try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)

How can I include a class that is in the parent directory using Kohana PHP

I made a helper class which have lots of small functions that will help me to create my content, but when I try to include it in my code the PHP shows an error saying that my class doesn't exist.
I just use require_once('../general.php'); but it gives me a "failed to open stream" error.
Just add the class to application/classes and use as normal. I've used some kind of Util.php class with some static functions like that.
Oh and don't bother with loading it manually, autoloader should deal with it just fine.
Edit:
Make sure that your class starts with a capital letter (General.php) and call it just General in your code.
You just need to call it just by name like if you have Function.php
You can call it through (Function) no need to add php extension.
you should use General not General.php

PHP Class Not Loading

I'm using WordPress and I'm trying to write a plugin that uses a file from another plugin. The URL to get the file is correct. I'm able to include the file when I'm calling a static function from the class, it is saying that the class is not loading.
//loading the file
$url=plugins_url().'/nextgen-gallery/admin/functions.php';
include $url;
The filename is functions.php and in it is a class defined nggAdmin
However, when i call the function nggAdmin::create_gallery();, i get the following error:
Fatal error: Class 'nggAdmin' not found in /var/www/html/wordpress/wp-content/plugins/Product/addProductForm.php on line 27
plugins_url() gives you the url, e.g. http://example.com/wordpress/wp-plugins/, not the server path - http://codex.wordpress.org/Function_Reference/plugins_url
Use WP_PLUGIN_DIR instead - http://codex.wordpress.org/Determining_Plugin_and_Content_Directories

Categories