I'm trying to load the Google calendar API which comes in PHP, but firstly to load the API, I need to pass the apiClient class instance to another class, I'm having no success at this since I'm using Codeigniter to load the apiClient library, but it fails to load the second class.
What I've done:
$this->load->library('google/apiClient', NULL, 'apiClient');
echo gettype($this->apiClient); // object
// This class needs the `apiClient` class to be passed to this, but fails
$this->load->library('google/contrib/apiCalendarService', $this->apiClient);
The error that gets thrown is this:
Argument 1 passed to apiCalendarService::__construct() must be an instance of apiClient, none given
However, if I do this:
$this->load->library('google/contrib/apiCalendarService', array($this->apiClient));
It will throw this error (passed the $this->apiClient in an array):
Argument 1 passed to apiCalendarService::__construct() must be an instance of apiClient, array given
What is the problem here? How can I specifically pass the apiClient to the apiCalendarService class?
The problem here is that CodeIgniter expects an array as the first parameter passed to all libraries, so not all classes fit in by default as CI libraries (particularly, ones that expect constructor parameters).
I see two options here:
Load the darned thing the "old way": include the class and instantiate new apiCalendarService($this->apiClient)
Create a wrapper class to "bridge the gap" between the two incompatible frameworks
Option #1 is two lines of code, by the way.
Related
I have TestController, hich doesn't have its own model. I use various model inside it, among them Trunk Model. I have my function "call", that wants to use method "singlePckgCall" from Trunk Model. Until here everything goes okay.
This method, which I'm trying to use, uses another method, placed below - "callSingleNumber".
And then it stops, and sends me an error page:
Unknown Method – yii\base\UnknownMethodException
Calling unknown method:
frontend\controllers\TestController::callSingleNumber()
I have no idea why it wants to call a method from TestController, instead on Trunk Model, as I want it to do. In result, it doesn't see such a method, because it exists in another class. I tried to rewrite this part manually once more, but it didn't help.
You are calling singlePckgCall statically, so inside singlePckgCall method $this instance is not available.
To solve your problem, you can
1) call callSingleNumber statically in singlePckgCall :
$action_id_array[$key] = self::callSingleNumber($numery[$i], TRUE);
at row 52
2) create an instance of Trunk class, so $this is available in singlePckgCall:
$instance = new Trunk();
$instance->singlePckgCall($numery);
I have a class called GeneralReport, implementing HttpAccessibleDataGathererInterface, with a constructor and a method called calculate() that builds an array of plain PHP objects, containing the fields for a report. GeneralReport accepts two constructor arguments: one is an array of parameters, which contains the input used to construct the query. The second argument is another class, GeneralReportQueryBuilder, to build the query for the report based on the first argument.
Now, I would like the second argument to always be injected (I.E. the GeneralReportQueryBuilder). However, I want to somehow be able to pass in the first argument to the GeneralReport class in my controller. Keep in mind that the GeneralReport class implements an interface, so passing the array as an argument to the calculate() method is not an option. Also, since calling this class without an array of input would be pointless, it is not an optional dependency and creating a setter for it wouldn't make sense.
I would like to make the dependencies of GeneralReport clear and concise using its constructor, and don't want the DIC to get in the way.
There is a difference between :
creating a instance without X parameter
using an instance without X parameter
The first assertion is resolved by constructors parameters, the second by throwing an exception if during execution the parameter is not filled or valid!
Contextual parameters can be sent by setters !
I have a bootstrap class which I want to use to set CSS variables:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView()
{
$this->bootstrap('view');
...
...
}
}
But trying to get the view resource fails at the bootstrap('view') stage. I get the error:
... Circular resource dependency detected' in C:\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php on line 662
...
Which is strange because this is the procedure that tutorials(and the zend documentation) use. What could be wrong?
Change the method to something like _initViewStuff() and all will be fine.
The reason is that the bootstrap sequence in Zend_Application_Bootstrap_BootstrapAbstract is as follows:
Your initial call to $app->bootstrap() in public/index.php runs through all _initXxx() methods (#see Zend_Application_Bootstrap_BootstrapAbstract::getClassResourceNames()) and calls $this->bootstrap('xxx') for each Xxx it finds. It will then do a similar thing for all the plugin resources defined by resources.* keys in application.ini (though yours never gets that far, as described below).
The call to bootstrap('view') internally stores a flag that he has started the process to bootstrap a resource called view.
He does a similar thing as (1), looking for a matching _initXxx() method. He find it and attempts to execute $this->_initView()
He notices the flag he set, indicating that he's gonna hit an infinite loop, so he bails out with circular dependency exception.
Typically, for each resource xxx, you bootstrap it using one (but not both, as you have discovered) of the following approaches:
Define an _initXxx() method.
Creating a plugin resource class named something like My_Application_Resource_Xxx (you inform that system that My_Application_Resource_ is a namespace prefix for plugin resources using pluginPaths.My_Application_Resource = /path/to/dir/containing/plugin in application/configs/application.ini)
You cant use this method name in your bootstrap class '_initView' because there is corresponding Zend_Application_Resource_View, just rename your bootstrap method name
I'm trying to create a mock to satisfy a typehint with this code (Mockery):
return \Mockery::mock('\Contracts\Helpers\iFileSystemWrapper');
or this (PHPUnit):
return $this->getMock('\Contracts\Helpers\iFileSystemWrapper');
But the mock returned is called Mockery\Mock Object or Mock_iFileSystemWrapper_a5f91049. How am I supposed to type check this when it isn't an instance of what I need at all with either framework?
Why exactly is the mock framework trying to load the real class? If I wanted the real class I would include the real class.
This problem has slowed me down so many times when writing tests I'm about to just toss type hinting out the window and check class names instead, or simply use production objects as mocks are a pain to use.
I just experimented with an existing test of my own, and by changing the interface namespace name from one that exists to one that doesn't exist, I got exactly the same as what you describe (using phpunit). My mock object had the class name Mock_ViewInterface_c755461e. When I change it back to the correct interface name, it works fine.
Therefore I would say that either:
You are trying to use an interface name that doesn't exist (e.g. a typo or missing namespace component).
Your library code isn't being loaded for some reason, e.g. autoloading is not setup correctly in your unit test bootstrap.
You need use a special function to check base class.
Somthing like this:
$mock = $this->getMock('MyClass');
$this->assertInstanceOf('MyClass', $mock);
I'm using CI and I have a UserModel that selects the user based on login information and sets a userVO and add this userVO in a session like this:
$this->session->set_userdata('user', $userVO);
When I try to access this session it return me this error:
Message: main() [function.main]: The script tried to execute a method
or access a property of an incomplete object. Please ensure that the
class definition "UserVO" of the object you are trying to operate on
was loaded _before_ unserialize() gets called or provide a __autoload()
function to load the class definition.
I have found a "solution", I need CI to load the UserVO class before session class and it works.
The problem is that I have lots os VO classes and I'll need them inside the session and is a bad thing to autoload them because I won't need them all at the same time.
Is there any workaround?
Thanks in advance for any help.
Whats going on is that you are saving an instance of the class to the session. In order to restore it, you first need to load the base class it is an instance of. You likely have lots of "instances" of the VO class, rather than lots of VO classes. You just need to load the file that has the class declaration.
The class instance really only contains what has changed from the base class, not the whole class. So it needs the underlying class to know what the "defaults" are.
I assume $userVO is a Model
$this->session->set_userdata('user', $userVO);
use
$this->session->set_userdata('user', json_encode($userVO));