Jump to content

Override class error


Teskuroi

Recommended Posts

I create a module and put override file "Address.php" to "/override/classes/" in my module directory. When I click install in ACP, Prestashop made file /override/classes/Address.php in root, and plant contents like this:

	/*
	* module: nkpitanie
	* date: 2015-02-13 10:40:43
	* version: 1.0.0b
	*/
public static  $definition = array
	(
   
// another contents...
			),
	);

And when I click "Uninstall" Prestashop delete just a one line. After uninstalling, file /override/classes/Address.php looks like:

	/*
	* module: nkpitanie
	* date: 2015-02-13 10:40:43
	* version: 1.0.0b
	*/
	(
   
// another contents...
			),
	);

And, off course, in next time, when i try to install my module Prestashop return an error. How do i fix it?

 

UPDATED 2015.02.15:

SOLUTION:

Correct way to override static variables in Prestashop classes:

<?php
class Address extends AddressCore
{	
    public function __construct($id = null, $id_lang = null)
    {   
        self::$definition['fields']['address1'] = array('type' => parent::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128);
        parent::__construct($id, $id_lang);
    }
} 
Edited by Teskuroi (see edit history)
Link to comment
Share on other sites

I'm not sure you can override a static variable.  What i would suggest you do is instead override the getDefinition function and not use the $definition variable directly.

 

The getDefinition function is a public static function in the ObjectModel class.  Address extends ObjectModel but does not override it, so your Address Override class should be able to override and then add or change whatever you need to do, before returning it

Link to comment
Share on other sites

Thanks for your help. Your method too difficult for this task, i guess.

I found more elegant way:

<?php
class Address extends AddressCore
{	
    public function __construct($id = null, $id_lang = null)
    {   
        self::$definition['fields']['address1'] = array('type' => parent::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128);
        parent::__construct($id, $id_lang);
    }
} 

P.S. PHP supports overriding static variables:

<?php
class A
{
	public static $variable = 'Old value.';
}

class B extends A
{
	public static $variable = 'New value.';
}

echo B::$variable; // return correctly: New value.

Anyway thank you bellini13.

Link to comment
Share on other sites

  • 4 years later...

Hi,

Put all public static $definition in a single row.

For example:

If you have

public static $validators = array(
        'active' => array('AdminImportController', 'getBoolean'),
        'tax_rate' => array('AdminImportController', 'getPrice'),
        'price_tex' => array('AdminImportController', 'getPrice'),
        'price_tin' => array('AdminImportController', 'getPrice'),
        'reduction_price' => array('AdminImportController', 'getPrice'),
        'reduction_percent' => array('AdminImportController', 'getPrice'),
        'wholesale_price' => array('AdminImportController', 'getPrice'),
        'ecotax' => array('AdminImportController', 'getPrice'),
        'name' => array('AdminImportController', 'createMultiLangField'),
        'description' => array('AdminImportController', 'createMultiLangField'),
        'description_short' => array('AdminImportController', 'createMultiLangField'),
        'meta_title' => array('AdminImportController', 'createMultiLangField'),
        'meta_keywords' => array('AdminImportController', 'createMultiLangField'),
        'meta_description' => array('AdminImportController', 'createMultiLangField'),
        'link_rewrite' => array('AdminImportController', 'createMultiLangField'),
        'available_now' => array('AdminImportController', 'createMultiLangField'),
        'available_later' => array('AdminImportController', 'createMultiLangField'),
        'category' => array('AdminImportController', 'split'),
        'online_only' => array('AdminImportController', 'getBoolean')
    );

Move in a single row, outside of __construct function but inside of class or controller.

public static $validators = array('active' => array('AdminImportController', 'getBoolean'),'tax_rate' => array('AdminImportController', 'getPrice'), rest of code... ));
 

and uninstall will work fine

Edited by igpetru (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...