PHP Class Not Loading - php

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

Related

Class being redeclared after include

I have the following include code on a page:
include "cc/ConstantContact.php";
This file exists and contains the lines:
require_once('Authentication.php');
require_once('Collections.php');
require_once('Components.php');
When I load the file that includes constantContact.php, I get the following error:
Fatal error: Cannot redeclare class OAuthException in /public_html/cc/Authentication.php on line 13
I do not understand how this class is being declared twice, as there is only the one time that the constantContact.php file includes Authentication.php.
These includes work just fine for me on other sites as well.

PHP - Fatal error: Cannot redeclare class Config in /path/to/Config.php on line 44

This is a WordPress local installation that I am trying to work with. I have not written a single line of this code myself. I don't understand what this error means:
Fatal error: Cannot redeclare class Config in /Applications/XAMPP/xamppfiles/lib/php/Config.php on line 44
Line 44 reads as follows:
class Config {
My guess is that a Config class has either already been declared elsewhere, or that this file is being executed for the second time.
That usually happens when you declare a class more than once in a page -- maybe via multiple includes.
To avoid this, use require_once instead. If you use require_once PHP will check if the file has already been included, and if so, not include (require) it again.
Say, for example, you have the following code:
<?php
class foo {
# code
}
... more code ...
class foo { // trying to re-declare
#code
}
In this case, PHP will throw a fatal error similar to the one below:
Fatal error: Cannot redeclare class foo in /path/to/script.php on line 7
In this case it's very simple -- simply find the 7th line of your code and remove the class declaration from there.
Alternativey, to make sure you don't try to re-declare classes, you can use the handy class_exists() function:
if(!class_exists('foo')) {
class foo {
# code
}
}
The best approach, of course, would be to organize all the configurations in one single file called config.php and then require_once it everywhere. That way, you can be sure that it will be included only once.
As for debugging the error, you could use debug_print_backtrace().
It's possible that the theme you are using refers to a file called config.php. If so use the following steps.
Try to find the config.php file and change it's name to configuration.php.
Find the files where they use config.php in the code and change it to configuration.php.

Can't call classes if they are in another php file?

I am attempting to call a class in a required file, but I am getting the error
PHP Fatal error: Class 'SampleClass' not found in /home2/domain/public_html/website/v2/wp-content/plugins/myplugin.php on line 23
I can tell that the file is required correctly as I have echoed some text in it and it displays. I am using Wordpress - is there anything weird about Wordpress which would prevent this from working?
As soon as I copied the class into the original file (as opposed to including it from somewhere else) it works fine.
Any ideas? Can provide more info if needed, but I'm opposed to copying the code it here because it would be mostly irrelevant.
To use that class you have to prefix the class name with the namespace for example:-
Instead of:
new Codebird;
You would write:
new Codebird\Codebird;

codeigniter include common file in view

Hi all I have a site developed in codeigniter and I wanto to store into a file called common.php some javascript/PHP function that I use in many pages.
I have tried in this mode:
require(base_url().'application/libraries/common.php'); //I have tried also include
This return me this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
I'm going to my php.ini and I turn On allow_url_include, restart apache and when I try to load the page return me now this error:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
Filename: backend/hotel_view.php
Line Number: 6
A PHP Error was encountered
Severity: Warning
Message: require(http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php) [function.require]: failed to open stream: no suitable wrapper could be found
Filename: backend/hotel_view.php
Line Number: 6
Fatal error: require() [function.require]: Failed opening required 'http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/public/klikkahotel.com/application/views/backend/hotel_view.php on line 6
What can I do to include a simple file into my pages?
Pull it into whichever views you want using $this->load->view('common'); You can include other views from either the controller or the view.
Example 1
your_controller.php
public function index() {
$this->load->view('homepage');
}
views/homepage.php
<?php
$this->load->view('common');
?>
<body>
<!-- html -->
</body>
Example 2
your_controller.php
public function index() {
$this->load->view('common');
$this->load->view('homepage');
}
You should use APPPATH or BASEPATH or just type the full path to the file.
For security, require_once should be passed a local file, not a URL. I wouldn't really suggest using require_once() in CodeIgniter. It might be better to use:
$this -> load -> view('common_file');
How To Make Global Functions In CodeIgnitor
To create global functions, define them in CodeIgnitor Helper files and auto load them. Here's how:
Create Your Helper
To create [helpers][2], create a .php file in the application/helpers/ folder and save your functions there.
Note: It is good practice to use the following format, to avoid function name collisions:
if ( ! function_exists('my_function_name'))
{
function my_function_name($arg)
{
/* Your code here */
}
}
Making Them Global
If you use these functions all the time (Global), auto-load them.
Open the file: /config/autoload.php
Find the section $autoload['helper'] = array();
Add the name of your helper file (excluding the.php extension).
For instance, if you created a helper file called myhelper.php, It should look something like this:
$autoload['helper'] = array('myhelper');
Using Your Global Functions
Now your functions will be available throughout the site. Just call them wholesale:
my_sample_function('argument');
base_url() refers to the web path like http://localhost/myproject/. You cannot include a remote file, actually you should not. It's a security risk. See Can't include file on remote server
Building a custom library is a good choice and if you are using it a lot in your website, you can include it in application/config/autoload.php under the section $autoload['libraries']. It will autoload every time you reload the application/website based on codeigniter. Example: $autoload['libraries'] = array('common'); if your library is called common.php and is located in application/libraries/
DO NOT put functions into a viewer, that's why libraries and helpers exists. A viewer should contain only what a user should see. Example: a view is some form of visualisation of the model.
You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');
You Can include anyware using
<?php $this->load->view('file'); ?>
For solve in more efficient way this problem I have done so:
You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
In your controller you call only one view in respect of MVC and call the functions from your custom helper.
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
You can write php function in helper file
steps - create a helper file name common_helper inside application/helperfolder
- and create a function like
function getBoosters($id){
$ci=& get_instance();
$ci->load->database();
$sql = "select * from boosters where id ='".$id."' ";
$query = $ci->db->query($sql);
return $query->result();
}
This common function you can use where you want by loading this helper.
Suppose you want to use this method in FrontController simply load the helper by this line
$this->load->helper('common');
Now you can call the method. Add your js code in footer.php, all the functions are available on every page you can easily use them

wordpress page template requre_once() failed

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(); ?

Categories