Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to include a class within another class correctly? WordPress

  • SOLVED

Hi,

I am the developer of the www.mapsmarker.com mapping plugin and due to the large amount of options my plugin offers and how the settings page is constructed, a lot of php memory is consumed within the admin backend. As a complete overhaul of the settings page is currently not possible, I try to reduce the memory load by trying to call the settings class "Class_leaflet_options" only when needed (which is when the settings page is loaded and when the function lmm_install_and_updates() is run). Unfortunately this fails - here is what I tried so far:

Before (leaflet-maps-marker.php):
<?php
...
if ( is_admin() ) {
}
require_once( plugin_dir_path( __FILE__ ) . 'inc' . DIRECTORY_SEPARATOR . 'class-leaflet-options.php' );
}
class Leafletmapsmarker
{
...
function lmm_settings() {
$lmm_options = new Class_leaflet_options();
$lmm_options->display_page();
}
...


After my try to require only when needed:
<?php
...
if ( is_admin() ) {
}
//require_once( plugin_dir_path( __FILE__ ) . 'inc' . DIRECTORY_SEPARATOR . 'class-leaflet-options.php' );
}
class Leafletmapsmarker
{
...
function lmm_settings() { //function which is called when settings page is opened
require_once( plugin_dir_path( __FILE__ ) . 'inc' . DIRECTORY_SEPARATOR . 'class-leaflet-options.php' );
$lmm_options = new Class_leaflet_options();
$lmm_options->display_page();
}
...


The settings class within /leaflet-maps-marker/inc/class-leaflet-options.php remains as it is.

Unfortunately when opening the plugin settings page (http://domain/wp-admin/admin.php?page=leafletmapsmarker_settings) after trying to optimize, the display of the the panes/sections is broken (see attached before/after image below)

FYI: loading the class only when the settings page is displayed is also not an option:

if ( is_admin() ) {
$current_page = isset($_GET['page']) ? $_GET['page'] : '';
if ($current_page == 'leafletmapsmarker_settings') {
}
require_once( plugin_dir_path( __FILE__ ) . 'inc' . DIRECTORY_SEPARATOR . 'class-leaflet-options.php' );
}


This wouldnt solve my problem as there is the other function lmm_install_and_updates() { which wont work if class-leaflet-options.php isnt loaded before. And here I cant make a page check like for settings display page as this function is loaded once a day regardless on which page the user is on.

Although I spent many hours finding out why, I dont know why my settings page breaks and would need help here - I guess the solution has to be found within /inc/class-leaflet-options.php where I use the settings api to handle options.
Thanks,

Robert

Answers (2)

2013-04-14

Yakir Sitbon answers:

Do you try use spl_autoload_register() for include classes, when its going use only?
You can start with nice gist from here: https://gist.github.com/Rarst/5049915

If you need more help, i can help you in PM.

Yakir.


Yakir Sitbon comments:

BTW, Why you are create two objects by this class?

class-leaflet-options.php, line: 8215.
$leafletmapsmarker_options = new Class_leaflet_options();
function lmm_option( $option ) {
$options = get_option( 'leafletmapsmarker_options' );
if ( isset( $options[$option] ) )
return $options[$option];
else
return false;
}


Robert Seyfriedsberger comments:

Hi Yakir,
thanks for gist - I will take a look although I am not quite sure how to deal with this code...

regarding two objects by this class: to be honest - I am not sure, this is really some legacy code I tried to optimize for a while - what exactly is wrong with it or how should I solve this?
Thx,
Robert

2013-04-14

Daniel Yoen answers:

I got "[14-Apr-2013 18:17:02 UTC] PHP Fatal error: Class 'Class_leaflet_options' not found in /my-home/wp-content/plugins/leaflet-maps-marker/inc/install-and-updates.php on line 134"

try this in install-and-udates.php file

function __autoload($class_name) {
require_once( plugin_dir_path( __FILE__ ) . 'inc' . $class_name . '.php' );
}


hope this help :-)


Robert Seyfriedsberger comments:

sorry - didnt test my zip package - with outcommended require install-and-udpates.php wont run and therefore the install of the plugin will fail. Sorry didnt think about that when creating the package. Please test with normal package.

Where should I add your code? As my knowledge here is limited, I'd appreciate your help here.
Thanks


Daniel Yoen comments:

inside install-and-udates.php file, but I think it doesn't resolve your problem, sorry

In your case, you can't include file inside body class, you should try another methods :-)


Daniel Yoen comments:

Sorry, I mean class body