problem whith register

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

problem whith register

Post by nahydy »

hi
i'm using zend framework and i'm trying to use the register to save parameters. i wrote this code in my controllers :

Code: Select all


                Zend::register('var','dddmm');
		$var1=Zend::registry('var');
		$this->view->message=$var1;



and when i tried to run my application i've got this error message:
Fatal error: Class 'Zend' not found in /var/www/test/pixels/application/controllers/IndexController.php on line 126
so i thought that may be in my function init i should add this

Code: Select all

Zend_Loader::loadClass('Zend_Registry');
but it still shows the same problems i dont' understand why it doesn't works. can you help me.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you have

Code: Select all

require 'Zend.php'
or

Code: Select all

include 'Zend.php';
somewhere in your code?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

i checked in my code i didn't found this include. i added it and when i tried ti run again it seems that there's no such file.i looked into the zend framework files to make sure, and there is no such file. is that normal?
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

No it is not.
First you need setup Zend correctly before you start using it :wink: .
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

hi i downloaded the last release of Zend framework. Then i copied the library into my project. But it seems that there's no Zend.php. I'm little bit confused. :oops:
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Start from manual http://framework.zend.com/manual/en/ it can help you in many questions.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I don't have a Zend.php file in any of the versions of the Zend framework that I have.

In the few versions I have, it would appear that you would want to do something like:

Code: Select all

<?php
Zend_Registry::set('var','dddmm');
?>
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

thank you for your help in fact

Code: Select all

Zend_Registry::set('var','dddmm'); 
worked very well.
But i still have another problem. When i tested the set and the get i put all the code in one function so that i can see if my syntax do works. And it worked. But when i tried to use these two functions separately i've got the following problem: in my function getter_register, it can not recognize

Code: Select all

$this->tab_coords->get('coords_var')
.

Here the hole code:

Code: Select all

class caddie extends Zend_Registry 
{
	protected $tab_coords="";
	
	
function write_register($tab)
	{
	$this->tab_coords=$this->getInstance();
	$this->tab_coords->set('coords_var',$tab);	
	
	}
	
function getter_register()
	{
	
	return $this->tab_coords->get('coords_var');
	}
}

i thought that the register is accessible any where in my code. is there a problem in my code that i didn't see it?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You could try this:

Code: Select all

<?php
class caddie extends Zend_Registry
{
    protected $tab_coords="";
       
    function write_register($tab)
    {
        $this->tab_coords = parent::getInstance();
        $this->tab_coords->set('coords_var',$tab);     
    }

    function getter_register()
    {
        return $this->tab_coords->get('coords_var');
    }
} 
?>
Alternatively you could do:

Code: Select all

<?php
class caddie extends Zend_Registry
{
    public static function write_register($tab)
    {
        parent::getInstance()->set('coords_var',$tab);
    }

    public static function getter_register()
    {
        return parent::getInstance()->get('coords_var');
    }
} 
?>
And you can use it like:

Code: Select all

<?php
$tab = 'FooBar';
caddie::write_register($tab);

echo caddie::getter_register();
?>
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

hi i tried the solution that you proposed but it doesn't works. It shows me the following error message:
Strict Standards: Non-static method caddie::getter_register() should not be called statically, assuming $this from incompatible context in /var/www/test/pixels/application/controllers/IndexController.php on line 128

Fatal error: Uncaught exception 'Zend_Exception' with message 'No entry is registered for key 'coords_var'' in /var/www/test/pixels/library/Zend/Registry.php:145 Stack trace: #0 /var/www/test/pixels/application/models/caddie.php(35): Zend_Registry::get('coords_var') #1 /var/www/test/pixels/application/controllers/IndexController.php(128): caddie->getter_register() #2 /var/www/test/pixels/library/Zend/Controller/Action.php(497): IndexController->formulaireAction() #3 /var/www/test/pixels/library/Zend/Controller/Dispatcher/Standard.php(230): Zend_Controller_Action->dispatch('formulaireActio...') #4 /var/www/test/pixels/library/Zend/Controller/Front.php(889): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #5 /var/www/test/pixels/index.php(39): Zend_Controller_Front->dispatch() #6 {main} thrown in /var/www/test/pixels/library/Zend/Registry.php on line 145
any idea?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

ok for
Strict Standards: Non-static method caddie::getter_register() should not be called statically, assuming $this from incompatible context in /var/www/test/pixels/application/controllers/IndexController.php on line 128
it was obious i missed to put declare my function public static. But i still have the same problem. :oops:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Here's a clean stack trace so you can see what is happening:
Fatal error: Uncaught exception 'Zend_Exception' with message 'No entry is registered for key 'coords_var'' in /var/www/test/pixels/library/Zend/Registry.php:145
Stack trace:
#0 /var/www/test/pixels/application/models/caddie.php(35): Zend_Registry::get('coords_var')
#1 /var/www/test/pixels/application/controllers/IndexController.php(128): caddie->getter_register()
#2 /var/www/test/pixels/library/Zend/Controller/Action.php(497): IndexController->formulaireAction()
#3 /var/www/test/pixels/library/Zend/Controller/Dispatcher/Standard.php(230): Zend_Controller_Action->dispatch('formulaireActio...')
#4 /var/www/test/pixels/library/Zend/Controller/Front.php(889): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 /var/www/test/pixels/index.php(39): Zend_Controller_Front->dispatch()
#6 {main} thrown in /var/www/test/pixels/library/Zend/Registry.php on line 145
What are you trying to do with your app? Why do you need to extend the Zend registry?
nahydy
Forum Commoner
Posts: 25
Joined: Wed Aug 22, 2007 5:55 am

Post by nahydy »

what i'm trying to do is to save an array in my register. So i created a class which can set my array in a register. And through my controllers i will access to this register. In my controllers i did this:

Code: Select all



class IndexController extends Zend_Controller_Action
{
	function init()
{
$this->view->baseUrl = $this->_request->getBaseUrl();
 Zend_Loader::loadClass('caddie');

}

function indexAction()
{
$this->view->title = "Mespixels";
$this->view->baseUrl=$this->_request->getBaseUrl();

}

function formulaireAction()
{  
	$this->view->title = "formulaire d'inscription";
	
          if ($this->_request->isPost()) 
	{
		$res=$this->_request->getPost('tabs_coords');
		$fields= explode(',',$res); 
		caddie::write_register($fields);
		$this->render();
	}
}

function supprimerAction()
{
$this->view->title = "Supprimer un album";
$this->view->baseUrl=$this->_request->getBaseUrl();
$this->view->message=caddie::getter_register();
$this->render();

}
}


My two functions works well if i used them in my formulaireAction but when i tried to get the value stored in my register through my supprimerAction here i've got the error message . the aim of using my register is to be accessible anywhere in my code application. i don't understand why it doesn't works like this? can you help me.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You must call caddie::write_register() before you call caddie::getter_register() otherwise you are going to get that error.

The problem is that when the supprimerAction() method gets called the formulaireAction() method is not getting called before it. This is because only one controller action is getting called on each page load in your framework, which is how it is usually supposed to work unless you are using _forward() or some fancy routing.

If you are trying to get the variable to stick around between pages, then you don't want to use Zend_Registry, you want to use Zend_Session to set and retrieve the value.
Post Reply