Using Zend Framework components in CI |
[eluser]JoostV[/eluser]
Hi all, Fred Wu posted a good tutorial on using Zend Framework components in your CI app. He created a library that you only need to load if you're going to use it. This means no overhead until you actually call the class (unlike the hook used by Daniel Vecchiato -who is greatly admired nonetheless- that sets your include_path whether you use ZF or not) Zend Framework has component with great functionality, such as - API classes for use with numerous web services such as Flickr, GData, Twitter, etc - A great Caching class, that also does partial caching and function caching - Session and Auth classes - Feed parser class - Etc. A lot of these classes can be used stand-alone. After you've copied ZF and created the Zend library class, here's an example controller zendtest.php that uses Zend_Service_Flickr and Zend_Cache Code: <?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
[eluser]JoostV[/eluser]
[EDIT] The controller should not be named zendtest.php but test.php
[eluser]mihailt[/eluser]
probably a slightly change would improve it Code: <?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
[eluser]JoostV[/eluser]
Ah, you placed it in a folder 'extensions'. That's a better place, indeed. Thnx.
[eluser]mihailt[/eluser]
the main improvement is usage of Zend_Loader class over direct require_once call.
[eluser]Phil Sturgeon[/eluser]
I was looking into a way to make it use normal CI syntax, meaning the following would be possible: Code: $this->zend->load('flickr', array('API_KEY')); But call_usr_func_array() just doesnt work with constructors :'( Code: // EPIC FAIL Anyone got a way round it? If we could get these classes called in the loader with all arguments passed as in my example we wouldn't need to call $flickr = new Zend_Service_Flickr($flickr_api_key); in our controllers. Must admit I don't know much about Zend, but would learn a little more if it would get on better with my friend.
[eluser]JoostV[/eluser]
How about if you define the parameters in your construct? Code: function __construct($classes = array()){
[eluser]Randy Casburn[/eluser]
[quote author="pyromaniac" date="1229111863"] But call_usr_func_array() just doesnt work with constructors :'( ...Anyone got a way round it? [/quote] This is true because the callback function is being used on an object. In order for you to call a user defined function (__construct() in this case) inside an object with a callback function, the method must be a static method. There are two ways to call the method. One requires the object to be instantiated first, prior to the callback function being invoked. Since the __construct() method is called during instantiation, that kinda shoots your design in the foot. The other requires the Class and method to be called statically. First let's set up the Test class since it is exactly the same either way... Code: class Test { Here are the two ways to accomplish what you want to do: With a static Class call to a static method: Code: call_user_func_array('Test', 'set_parameters', array('param1', 'param2')); With a call to a static method of an instantiated class: Code: $oTest = new Test(); I hope you find this helpful, Randy
[eluser]fredwu[/eluser]
[quote author="mihailt" date="1229107610"]the main improvement is usage of Zend_Loader class over direct require_once call.[/quote] From the Zend Framework user guide: Quote: Zend_Loader vs. require_once() I think it really depends on the usage to justify it as an 'improvement'.
[eluser]Phil Sturgeon[/eluser]
You guys slightly missed the point but gave very good answers. The point is im trying to call the constructs of the Zend library files, so I obviously do not want to make and modifications to them. That means I cannot call a different method, or modify the constructor. I tried both of those before realizing it would be no help, and as far as I know there is no way to do it in PHP. So, I'm not bothering! hut: |
Welcome Guest, Not a member yet? Register Sign In |