Jump to content

Autoloading not working within class


ballhogjoni
Go to solution Solved by ballhogjoni,

Recommended Posts

I running a script which autoloads ShippingManager. An instance of ShippingManager calls a function that should create a new instance of another class that is located in the orders folder. I am getting Fatal error: Class 'MarketplaceWebServiceOrders_Client' not found. How do I fix this?

 

autoload code:

set_include_path(get_include_path()
        . PATH_SEPARATOR . 'orders'
        . PATH_SEPARATOR . 'feeds'
        . PATH_SEPARATOR . '../lib');

function __autoload($className) {
  $filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  $includePaths = explode(PATH_SEPARATOR, get_include_path());
  foreach ($includePaths as $includePath) {
    if (file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)) {
      require_once $filePath;
      return;
    }
  }
}

handle_shipping.php

<?php

include_once ('path/to/config.php');

$shipping_manager = new ShippingManager("ESHIP-" . date('Ym23') . ".txt");
if ($shipping_manager->getShippingFileFromFTP(URL, USER, PASS)) {
  $shipping_manager->createShippingCSVFile();
}

ShippingManager function that is called from the createShippingCSVFile function. The class MarketplaceWebServiceOrders_Client isn't being autoloaded.

  private function __getOrderItemId() {

    $service = new MarketplaceWebServiceOrders_Client();
    $orderItems = new MarketplaceWebServiceOrders_Model_ListOrderItemsRequest();
    $orderItemList = $orderItems->getOrderItem();
    foreach ($orderItemList as $orderItem) {
      print_r($orderItem->getOrderItemId());
    }
  }
Link to comment
Share on other sites

Either you have the wrong class name, your autoloader isn't working, or your autoloader isn't finding the right file. Given how little you've posted, I don't know which of those it is.

 

Have you tried... say... debugging? Your autoloader would be the first place to start.

Link to comment
Share on other sites

  • Solution

Either you have the wrong class name, your autoloader isn't working, or your autoloader isn't finding the right file. Given how little you've posted, I don't know which of those it is.

 

Have you tried... say... debugging? Your autoloader would be the first place to start.

 

Sorry i didn't post much, but your comment was all I needed. I realized that the ShippingManager function was calling MarketplaceWebServiceOrders_Client() from a different location. I just altered my include path to this:

set_include_path(get_include_path()
        . PATH_SEPARATOR . 'orders'
        . PATH_SEPARATOR . '../orders'
        . PATH_SEPARATOR . 'feeds'
        . PATH_SEPARATOR . '../feeds'
        . PATH_SEPARATOR . '../lib');
Link to comment
Share on other sites

For starters, this:

 

 

if (file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)) {
  require_once $filePath;
  return;
}

 

would need to be:

 

 

if (file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)) {
  require_once $includePath . DIRECTORY_SEPARATOR . $filePath;
  return;
}

 

But yeah, some debugging would help.

Link to comment
Share on other sites

 

Sorry i didn't post much, but your comment was all I needed. I realized that the ShippingManager function was calling MarketplaceWebServiceOrders_Client() from a different location. I just altered my include path to this:

set_include_path(get_include_path()
        . PATH_SEPARATOR . 'orders'
        . PATH_SEPARATOR . '../orders'
        . PATH_SEPARATOR . 'feeds'
        . PATH_SEPARATOR . '../feeds'
        . PATH_SEPARATOR . '../lib');

Oh, that looks terribly inefficient (and autoloading is a notorious bottleneck if not done well). Just use absolute paths to your libs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.