PrestaShop module classes not found (namespaces) - php

This is my PrestaShop module file structure:
-mymodule/
--src/
--mymodule.php
---Presta/
---Webhooks.php
----Controller/
-----MyPrestaController.php
mymodule.php cannot find Webhooks.php class, I've tried use in mymodule.php, but still it provides errors:
ClassNotFoundException in mymodule.php line 55:
Attempted to load class "Webhooks" from namespace "src\Presta".
Did you forget a "use" statement for another namespace?
When I try to use autoload/include/require in mymodule.php it throws fatal errors, because autoload initialize stuff(from my module vendor) that shouldn't be initialized in mymodule.php. GuzzleClient gets crazy while browsing website:
Catchable Fatal Error: Argument 3 passed to
GuzzleHttp\Client::request() must be of the type array, string given,
called in /usr/local/ampps/www/presta/modules/mymodule/vendor/guzzlehttp/guzzle/src/Client.php on line 89 and defined
I don't want to put all hook logic in mymodule.php and I have other classes that I need to implement in webhook methods. Is there any way to use other classes in main module file(mymodule.php)? Am I missing something?

You need to call your class with full path or declare a use on top of your module file. I don't know exactly what namespace Webhooks is under but something like this:
public function hookActionAuthentication($params)
{
\src\Presta\Webhooks::myStaticWebhooksMethod($params);
}
or
use src\Presta\Webhooks; // before module class declaration
public function hookActionAuthentication($params)
{
Webhooks::myStaticWebhooksMethod($params);
}

Related

php - how to remove namespace when calling object from independent class?

I'm calling an independent class's static function like: AnimeshPlugin::get_plugin_basename();
AnimeshPlugin is defined class... I'm using namespace AniPlugin\custom;
and when i call this function within this namespace class, error occured as:
Fatal error: Uncaught Error: Class 'AniPlugin\custom\AnimeshPlugin'
not found in
C:\xampp\htdocs\wordpress\wp-content\plugins\animesh-plugin\includes\config\custom\admin.php
on line 25
I don't want to add AniPlugin\custom\ before the class name.
Any suggestions??
Try the following:
\AnimeshPlugin::get_plugin_basename();
Look into Importing Namespaces in PHP and Global Space

ZF 1.12 custom view helper - failed opening

I'm working on restructuring my Zend 1.12 project. I have a couple of view helpers:
OutputComplexForm.php
OutputDistributorsList.php
I put them in /application/views/helpers
Class names are
Zend_View_Helper_OutputComplexForm
Zend_View_Helper_OutputDistributorsList
As I understand if you have Zend_View_Helper prefix you don't need to add any configs to application.ini
Now, when I try to load any page (even those which don't use helpers) I receive error:
Message: Zend_Session::start() - /otms/vendor/zendframework/zendframework1/library/Zend/Loader.php(Line:134): Error #2 include_once(): Failed opening 'Zend/View/Helper/OutputComplexForm.php' for inclusion (include_path='/otms/application/../library:/otms/application/../library/phpseclib0.3.1:/otms/application/../library/Amazon:/otms/application/../library/USPS:/otms/application/../library/Composer:/otms/library:/otms/vendor/phpseclib/phpseclib/phpseclib:/otms/vendor/zendframework/zendframework1/library:.:/usr/share/php:/usr/share/pear')
Did I miss something?
UPD
I've found out that error occurred after calling function
$startedCleanly = session_start(); //line 482
in file /Zend/Session.php. After this call property Zend_Session_Exception::$sessionStartError contains described error message. I still don't see connection between starting session and initialising view helper.
For application-specific classes that you write - stuff that appears inside ./application/* - should typically not be in the Zend_ pseudo-namespace. Rather, they should be in the appnamespace, as configured in ./application/config/application.ini.
The default namespace is 'Application_', so a view-helper called MyHelper would typically be stored in the file ./application/views/helpers/MyHelper.php:
class Application_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
// do your stuff here
}
}
Note that the class name is upper-camel-case MyHelper and the method is lower-camelcase myHelper().
In your view, you can invoke your view-helper with:
<?php
$output = $this->myHelper();
// Do something with $output
With these conventions on namespace, class name, and file location/name, and invocation syntax, the View's plugin loader should be able find, load, and execute your view-helper method.

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.

php " Class not found" and "Cannot redeclare class" error

I'm programming a website with PHP using Symfony2.
I defined a class MainContent in file MainController.php
And I had to use this class in other controller file named SecurityController.php
Although both of these classes are defined in the same namespace, it gave the error:
Class 'MainContent' not found...
So I tried to define the class again in SecurityController.php but result is:
Cannot redeclare class 'MainContent...'
I don't understand, it is either declared or not.
In fact I'm a C# programmer and maybe I'm confused because of some differences between these languages.
Symfony uses so-called autoloading. Since your class is not defined in it's own file, following the rules for auto-loading, it cannot be loaded, hence the class not found. But if you define the class, then script is processed successfully, other file that defines the same class is included, and the collision (cannot redeclare class) is generated.
The possible solutions are:
Define the class in one place (best outside the controller) and use include_once / require_once whenever you need it
Make the class actually a Symfony helper class, so autoloading actually works
Whenever you are declaring the class, use
if(!class_exists('ClassName')) {
class ClassName {}
}

Codeigniter extending exception class

I am trying to load a custom exception class that I created according to the instructions here:
http://codeigniter.com/user_guide/general/core_classes.html
MY_Exceptions.php is stored at application/core/
Somehow, when I try loading it, I keep getting this error:
Fatal error: Class 'MY_Exceptions' not found in C:\xampp\htdocs\xampp\facebook\application\models\campaign_model.php on line 29
Based on the instructions, it doesn't say I have to autoload the class or anything. What am I doing wrong?
MY_Exception.php is stored at application/core/
Name the class and file MY_Exceptions with an s
You do not need to autoload or manually load anything in the core directory, nor should you. They are required classes for CI to run that are automatically loaded.
For creating core classes, use this documentation instead: http://codeigniter.com/user_guide/general/core_classes.html
Keep in mind that when calling the class you will use the original class name. Let's say you have created MY_Input. Example:
$this->input->post(); // Do this
$this->my_input->post(); // Don't do this
To understand the why and how, see system/core/Common.php function load_class.
Looking at the docs now, I agree that this should probably be highlighted.
It's trying to find MY_ExceptionS.php, not MY_Exception.php.

Categories