Call to a member function notifyPreDispatch() on a non-object in Action.php

View: New views
6 Messages — Rating Filter:   Alert me  

Call to a member function notifyPreDispatch() on a non-object in Action.php

by Olivier Ricordeau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi list,

I can't manage to get internalization working... I'm writing a class
that manages translation using ZendTranslate. Here is the class:


class Translator {

        protected $_translate;

        // Constructor
        public function Translator() {
                // Print the user's locale
                require_once "Zend/Locale.php";
                $locale = new Zend_Locale();
                print $locale->toString();

                Zend_Loader::loadClass('Zend_Translate');
                $_translate = new Zend_Translate('gettext', '../languages/fr.mo', 'fr');
        }

        public function _($msg) {
                if ($_translate->isTranslated($msg))
                        return $_translate->_($msg);
                else
                        return $msg;
        }

}

Now, here is my controller:

class IndexController extends Zend_Controller_Action
{

        protected $_trans;
       
        // Constructor
        public function IndexController() {
                $_trans = new Translator();
        }

        function init()
        {
                parent::init();
        }

        public function indexAction() {
                $this->view->title = _trans->_("Main page");
        }

}

Now here is what I get when I call the index from my browser:
Fatal error: Call to a member function notifyPreDispatch() on a
non-object in
/home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
on line 494

The locale isn't even displayed (when I do "print $locale->toString()"
in the Translator's class' constructor.

Can anybody help me with that?

--
- *Olivier RICORDEAU* -
  olivier@...

Re: Call to a member function notifyPreDispatch() on a non-object in Action.php

by Olivier Ricordeau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Olivier Ricordeau a écrit :

> Hi list,
>
> I can't manage to get internalization working... I'm writing a class
> that manages translation using ZendTranslate. Here is the class:
>
>
> class Translator {
>
>     protected $_translate;
>
>     // Constructor
>     public function Translator() {
>         // Print the user's locale
>         require_once "Zend/Locale.php";
>         $locale = new Zend_Locale();
>         print $locale->toString();
>
>         Zend_Loader::loadClass('Zend_Translate');
>         $_translate = new Zend_Translate('gettext',
> '../languages/fr.mo', 'fr');
>     }
>
>     public function _($msg) {
>         if ($_translate->isTranslated($msg))
>             return $_translate->_($msg);
>         else
>             return $msg;
>     }
>
> }
>
> Now, here is my controller:
>
> class IndexController extends Zend_Controller_Action
> {
>
>     protected $_trans;
>    
>     // Constructor
>     public function IndexController() {
>         $_trans = new Translator();
>     }
>
>     function init()
>     {
>         parent::init();
>     }
>
>     public function indexAction() {
>         $this->view->title = _trans->_("Main page");

Remark:
It's actualy "$this->view->title = $_trans->_("Main page");"

>     }
>
> }
>
> Now here is what I get when I call the index from my browser:
> Fatal error: Call to a member function notifyPreDispatch() on a
> non-object in
> /home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
> on line 494
>
> The locale isn't even displayed (when I do "print $locale->toString()"
> in the Translator's class' constructor.
>
> Can anybody help me with that?
>


--
- *Olivier RICORDEAU* -
  olivier@...

Re: Call to a member function notifyPreDispatch() on a non-object in Action.php

by thomasW :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hy Olivier,

when you look at the returned error message you will notify that the error
occurs within the action controller and not within translation.
I expect that you have problems with your path, filenames or rewrite rules.
For details about solving MVC problems please write to the mvc list.

Wether Zend_Translate nor Zend_Locale are problematic in your case... if
they were, then you would have received an exception from one of them.

Greetings
Thomas Weidner, I18N Team Leader
http://www.thomasweidner.com


----- Original Message -----
From: "Olivier Ricordeau" <olivier@...>
To: <fw-i18n@...>
Sent: Wednesday, February 06, 2008 2:00 PM
Subject: [fw-i18n] Call to a member function notifyPreDispatch() on a
non-object in Action.php


> Hi list,
>
> I can't manage to get internalization working... I'm writing a class that
> manages translation using ZendTranslate. Here is the class:
>
>
> class Translator {
>
> protected $_translate;
>
> // Constructor
> public function Translator() {
> // Print the user's locale
> require_once "Zend/Locale.php";
> $locale = new Zend_Locale();
> print $locale->toString();
>
> Zend_Loader::loadClass('Zend_Translate');
> $_translate = new Zend_Translate('gettext', '../languages/fr.mo', 'fr');
> }
>
> public function _($msg) {
> if ($_translate->isTranslated($msg))
> return $_translate->_($msg);
> else
> return $msg;
> }
>
> }
>
> Now, here is my controller:
>
> class IndexController extends Zend_Controller_Action
> {
>
> protected $_trans;
>
> // Constructor
> public function IndexController() {
> $_trans = new Translator();
> }
>
> function init()
> {
> parent::init();
> }
>
> public function indexAction() {
> $this->view->title = _trans->_("Main page");
> }
>
> }
>
> Now here is what I get when I call the index from my browser:
> Fatal error: Call to a member function notifyPreDispatch() on a non-object
> in
> /home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
> on line 494
>
> The locale isn't even displayed (when I do "print $locale->toString()" in
> the Translator's class' constructor.
>
> Can anybody help me with that?
>
> --
> - *Olivier RICORDEAU* -
>  olivier@...


Re: Call to a member function notifyPreDispatch() on a non-object in Action.php

by Olivier Ricordeau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thomas Weidner a écrit :
> Hy Olivier,
>
> when you look at the returned error message you will notify that the
> error occurs within the action controller and not within translation.
> I expect that you have problems with your path, filenames or rewrite rules.
> For details about solving MVC problems please write to the mvc list.

No, I actualy had problems with my OO programming in PHP... But
everything is fine now. It's working. Sorry for my silly question.
I post the solution for the archives:

* Translator.php

class Translator {

        protected $_translate;

        public function Translator() {
                require_once "Zend/Locale.php";
                $locale = new Zend_Locale();
                print $locale->toString();

                Zend_Loader::loadClass('Zend_Translate');
                $this->_translate = new Zend_Translate('gettext',
'../languages/fr.mo', 'fr');
        }

        public function _($msg) {
                if ($this->_translate->isTranslated($msg))
                        return $this->_translate->_($msg);
                else
                        return $msg;
        }

}

* IndexController class:

        protected $_trans = null;

        public function init()
        {
                parent::init();
                $this->_trans = new Translator();
        }

        public function indexAction() {
                $this->view->title = $this->_trans->_("Main page");
        }

Cheers,

Olivier

> Wether Zend_Translate nor Zend_Locale are problematic in your case... if
> they were, then you would have received an exception from one of them.
>
> Greetings
> Thomas Weidner, I18N Team Leader
> http://www.thomasweidner.com
>
>
> ----- Original Message ----- From: "Olivier Ricordeau"
> <olivier@...>
> To: <fw-i18n@...>
> Sent: Wednesday, February 06, 2008 2:00 PM
> Subject: [fw-i18n] Call to a member function notifyPreDispatch() on a
> non-object in Action.php
>
>
>> Hi list,
>>
>> I can't manage to get internalization working... I'm writing a class
>> that manages translation using ZendTranslate. Here is the class:
>>
>>
>> class Translator {
>>
>> protected $_translate;
>>
>> // Constructor
>> public function Translator() {
>> // Print the user's locale
>> require_once "Zend/Locale.php";
>> $locale = new Zend_Locale();
>> print $locale->toString();
>>
>> Zend_Loader::loadClass('Zend_Translate');
>> $_translate = new Zend_Translate('gettext', '../languages/fr.mo', 'fr');
>> }
>>
>> public function _($msg) {
>> if ($_translate->isTranslated($msg))
>> return $_translate->_($msg);
>> else
>> return $msg;
>> }
>>
>> }
>>
>> Now, here is my controller:
>>
>> class IndexController extends Zend_Controller_Action
>> {
>>
>> protected $_trans;
>>
>> // Constructor
>> public function IndexController() {
>> $_trans = new Translator();
>> }
>>
>> function init()
>> {
>> parent::init();
>> }
>>
>> public function indexAction() {
>> $this->view->title = _trans->_("Main page");
>> }
>>
>> }
>>
>> Now here is what I get when I call the index from my browser:
>> Fatal error: Call to a member function notifyPreDispatch() on a
>> non-object in
>> /home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
>> on line 494
>>
>> The locale isn't even displayed (when I do "print $locale->toString()"
>> in the Translator's class' constructor.
>>
>> Can anybody help me with that?
>>
>> --
>> - *Olivier RICORDEAU* -
>>  olivier@...
>
>


--
- *Olivier RICORDEAU* -
  olivier@...

Re: Call to a member function notifyPreDispatch() on a non-object in Action.php

by Søren :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This error occurs when you define a constructor in your Controller_Action class. You should use the method preDispatch instead.

Olivier Ricordeau wrote:
Hi list,

I can't manage to get internalization working... I'm writing a class
that manages translation using ZendTranslate. Here is the class:


class Translator {

        protected $_translate;

        // Constructor
        public function Translator() {
                // Print the user's locale
                require_once "Zend/Locale.php";
                $locale = new Zend_Locale();
                print $locale->toString();

                Zend_Loader::loadClass('Zend_Translate');
                $_translate = new Zend_Translate('gettext', '../languages/fr.mo', 'fr');
        }

        public function _($msg) {
                if ($_translate->isTranslated($msg))
                        return $_translate->_($msg);
                else
                        return $msg;
        }

}

Now, here is my controller:

class IndexController extends Zend_Controller_Action
{

        protected $_trans;
       
        // Constructor
        public function IndexController() {
                $_trans = new Translator();
        }

        function init()
        {
                parent::init();
        }

        public function indexAction() {
                $this->view->title = _trans->_("Main page");
        }

}

Now here is what I get when I call the index from my browser:
Fatal error: Call to a member function notifyPreDispatch() on a
non-object in
/home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
on line 494

The locale isn't even displayed (when I do "print $locale->toString()"
in the Translator's class' constructor.

Can anybody help me with that?

--
- *Olivier RICORDEAU* -
  olivier@ricordeau.org

Re: Call to a member function notifyPreDispatch() on a non-object in Action.php

by Søren :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This error occurs when you define a constructor in your Controller_Action class. You should use the method preDispatch instead.

Olivier Ricordeau wrote:
Hi list,

I can't manage to get internalization working... I'm writing a class
that manages translation using ZendTranslate. Here is the class:


class Translator {

        protected $_translate;

        // Constructor
        public function Translator() {
                // Print the user's locale
                require_once "Zend/Locale.php";
                $locale = new Zend_Locale();
                print $locale->toString();

                Zend_Loader::loadClass('Zend_Translate');
                $_translate = new Zend_Translate('gettext', '../languages/fr.mo', 'fr');
        }

        public function _($msg) {
                if ($_translate->isTranslated($msg))
                        return $_translate->_($msg);
                else
                        return $msg;
        }

}

Now, here is my controller:

class IndexController extends Zend_Controller_Action
{

        protected $_trans;
       
        // Constructor
        public function IndexController() {
                $_trans = new Translator();
        }

        function init()
        {
                parent::init();
        }

        public function indexAction() {
                $this->view->title = _trans->_("Main page");
        }

}

Now here is what I get when I call the index from my browser:
Fatal error: Call to a member function notifyPreDispatch() on a
non-object in
/home/olivier/public_html/ZendFramework-1.0.3/library/Zend/Controller/Action.php
on line 494

The locale isn't even displayed (when I do "print $locale->toString()"
in the Translator's class' constructor.

Can anybody help me with that?

--
- *Olivier RICORDEAU* -
  olivier@ricordeau.org
LightInTheBox - Buy quality products at wholesale price!