The following code is the sample code:
$sample= $module->get('data')
->anotherModule
->find(true);
I couldn't understand what exactly is the object hierarchy in here. According to my knowledge, there must be an object $module and that class has a method get which takes the parameter as 'data'.
But according to the code, still it gets deeper to anotherModule and find method. Can anyone explain me what is happening in this code?
This is called method chaining.
A method returns an object which contains other methods. You can find an example for that here: PHP method chaining?
Related
I am writing a PHPspec test where want to test if a method was called with the correct argument (in the case an object).
I am doing something like this (pseudo code), inside my spec:
function it_some_test(MyService $myservice) {
$object = new MyDomainObject();
$object->setPropertyX("some value")
$myservice->getSomething($object)->shouldBeCalled();
}
The object is just a DTO created inside my service. Its created exactly the same way in the original Service. Just the value of some properties change based on same logic. thats what I want to test.
The problem is my test is always failing because it says the $object is different from what it was excepting but they have exactly the same properties values.
How does PHPspec compares objects? If I pass Argument::any() instead it works but I need to test the object passed as a property with a specific value.
I found the issue. I was calling the method ->shouldBeCalled() before calling the real method under test. Change the order and it works now.
In a custom validation function in my model class. I need to use javascript code. for that i used registerJs function but i am getting error:-
Calling to undefined function registerJs()
I also tried calling it by including View class i.e., View::registerJs() but it is also giving error called
Non-static method yii\web\View::registerJs() should not be called statically, assuming $this from incompatible context
How can i user Javascript in Yii2 model class.
Edit:
I have created a custom function for mobile number validation and calling that function from rules section of model. Now i want to use javascript code in that function. is there any other way to achive it?
Thanks in advance
That method is not static. If you open the view.php of the framework you can check out the implementation.
public function registerJs($js, $position = self::POS_READY, $key = null){..
}
Exception clearly mentions that should not be called statically because it is not declared static.
I have seen few of the implementations which call this method as:
$view->registerJs($js, $view::POS_END);
Basically loading a particular JS file in one of the functions.
It is not a good idea using Javascript with a model. If you go that way then probably after some time you will find you heading into big problems with the architecture of the app.
The best way is to call the model inside a controller and then interact with the controller through the Javascript code.
Im trying to build routing script in php.
Its currently working but now i want to add parameters and this is where im stuck.
Code im currently using:
$controllerName is the name of the class.
$action is the name of the method going to be called.
$controllerClass = new $controllername();
$controllerClass->$action();
The parameters i want parse into the method are in a array.
My problem is that in the method being called make references to (public) variables inside the object so if i use:
call_user_func_array()
im getting errors: Using $this when not in object context
Somebody got a good idea to solve this?
EDIT: Found my problem, when i was using the call_user_func_array() i was giving the class name as parameter rather than an object. That was why it was not in object mode but static mode.
you are using $this somewhere that is not within a class definition. $this should only be used within a class.
I'm just getting my feet wet in Wordpress, and I'm trying to write a very simple plugin using OOP techniques. I've been following this tutorial: http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/ . So far I feel like I understand most of what's going on, but I'm slightly puzzled by statements such as this one:
add_action('init', array(&$this, 'init'));
Having read the documentation on Wordpress's add_action() and PHP callables, I gather that the second argument is a method of a class instance. But I don't understand why $this has to be passed by reference.
Found this note in the PHP docs about callables which I suspect may have something to do with it, but I'm still having a hard time wrapping my head around what the difference is:
Note: In PHP 4, it was necessary to use a reference to create a callback that points to the actual object, and not a copy of it. For more details, see References Explained.
If I have PHP 5, am I safe just using array($this,'init')?
Possibly related: add_action in wordpress using OOP?
Yes - you are safe to just use array($this, 'init');.
What this is actually is, is a "callable" in PHP. It will be invoked using call_user_func() or call_user_func_array() (internally inside the add_action method).
PHP's official description of this type of callable:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
You can read more about callables here.
Is it possible to use doctrines magic methods (in this case findBy*) with sfDoctrinePager? I'm getting the following error:
Call to undefined method Doctrine_Collection::offset()
Here is the code:
$this->pager->setQuery(Doctrine::getTable('notification')->findByUserId($this->getUser()->getGuardUser()->getId()));
I know I can build a query myself just want to know if it's possible using the "magic" methods.
nope, you can't. the findby* methods returns an object or an array of objects. sfDoctrinePager can only work on query objects.