0

My programming background is in different languages so I am tackling php and magento and learning as I go. so this may be a php question more than magento , but it would be good to know how people write scripts like this.

require_once '../../../app/Mage.php';

Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
$newStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(newStoreId));

class myClass
{

    public function doStuff(){
        $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
        var_dump($this->installer);
//etc.. 
}

$obj = new myClass();
$obj-> doStuff();

$this->installer is always null. I have tried all sorts of different variations. I don't want $installer to have any initial value , but I have tried declaring as a public variable.

How can I set this value ( $installer ) inside the class and have access to it inside the class?

I feel like I am fundamentally missing something.

Thanks for your time.

  • Have you initialised myClass? – André Ferraz May 2 '18 at 15:10
  • yes @AndréFerraz . Sorry I have edited code to show that part. – Martin Thompson May 2 '18 at 20:29
0

Perhaps just do this ?

require_once '../../../app/Mage.php';
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
$newStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(newStoreId));

class myClass
{
    protected $_installer;

    function setInstaller($installer) {
        $this->_installer = $installer;
    }

    function getInstaller() {
        return $this->_installer;
    }

    public function doStuff(){
        $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
        $this->setInstaller($installer);

        var_dump($this->getInstaller());
    }
}

$obj = new myClass();
$obj->doStuff();
  • Sorry , the question was: How can I set this value ( $installer ) inside the class and have access to it inside the class? The whole idea is to do this in an OOP way - I know how to do that. Thank you. – Martin Thompson May 2 '18 at 20:24
  • I updated my answer. Now, you can access to $_installer with its getter inside the class. Is it that you want ? – Kozame May 3 '18 at 7:34
0

Taking a closer look at your example:

public function doStuff(){ $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup'); var_dump($this->installer);

should be:

public function doStuff(){ $installer = Mage::getResourceModel('catalog/setup', 'catalog_setup'); var_dump($installer);

or

public function doStuff(){ $this->installer = Mage::getResourceModel('catalog/setup', 'catalog_setup'); var_dump($this->installer);

Note that you're setting $installer as a variable and then attempting to reference it through a class member $this->classMember

For more information on variable scope in PHP see: http://php.net/manual/en/language.variables.scope.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.