Function uses method from different class than it should - php

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);

Related

How to use javascript in Yii2 model class?

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.

Using pingpong/widget in Laravel throws an error

I'm using the laravel's package
"pingpong/widget": "~2.1",
I've set the configuration in config\app.php
Added
Pingpong\Widget\WidgetServiceProvider::class,
and
'Widget' => Pingpong\Widget\WidgetFacade::class,
And as in the documentation, i'm using this code to create a widget:
use Pingpong\Widget\Widget;
Widget::register('small', function($contents)
{
return "<small>{$contents}</small>";
});
But I got this error:
Non-static method Pingpong\Widget\Widget::register() should not be called statically, assuming $this from incompatible context
In the Pingpong\Widget\Widget class, there are no static functions, so of course this error is expected. But... how we can create a widget then, when all sample codes in the documentation are using static classes and in the Widget class, there are no static methods? Am I missing something?
Creating a widget object and calling the ->register() on it also throw an error, because creating a widget expects BladeCompiler $blade, Container $container in the _construct method and I have no idea what to pass here.
Any ideas on what could be wrong with this implementation?
Change use Pingpong\Widget\Widget; to just use Widget;
Laravel, and some packages, include Facades to facilitate easy usage of some components. While everything looks like a static call, the static call on the facade actually maps to a non-static call on a concrete object that is masked by the facade. So, when you're attempting Widget::register(), the facade looks up the concrete instance it has of the Widget class, and then calls the register() method on that instance non-statically.
In your config/app.php file, you've added the Widget alias which points to the WidgetFacade. This alias is created in the base namespace. If you would like to use this alias to the facade, you need to just reference the Widget alias in the base namespace (i.e. use Widget;).
By stating use Pingpong\Widget\Widget;, your Widget::register() call is trying to call a static register() method on the Pingpong\Widget\Widget class, instead of the method on the facade.

Magento_beforeSave for customer entity

Magento calls _beforeSave function to process the object before saving the data. the access of the function is protected. (as per the magento documentation)
Was debugging how magento saves customer entity. _beforeSave data is getting called from Customer/Model/Customer.php which in turn calls parent::_beforeSave (Mage_Core_Model_Abstract).
But still unable to find from where the function is getting called. There must be some call to be made to run this function and where is it written??
The function always runs but can not find from where and how it is called??
This method is called in Mage_Core_Model_Abstract::save() method on line 316 just before calling the save() method in resource object.
So, when Magento call this method, it runs the overrided _beforeSave() method present in Mage_Customer_Model_Customer that calls Mage_Core_Model_Abstract::_beforeSave() (initialize object as new if not has an id and dispatch events) and execute the remaining code present in Mage_Customer_Model_Customer::_beforeSave()

Statically calling methods causes not being able to reference $this within called class?

I don't usually call methods statically but since working with the Yii framework I have started to more.
I'm experiencing an issue which I haven't come across before, I am doing this from a SignupForm class:
$send = mail::sendMail($email_data);
..inside the sendMail method which is obviously inside the mail class I have this line:
$email_data['message'] = $this->sanitizeMsg($email_data['message']);
The sanitizeMsg is a method of the mail class and hence that's why I thought referencing it via $this should work.
However I am getting the error:
Calling unknown method: app\models\SignupForm::sanitizeMsg()
Why is it looking for it in the SignupForm class? Does this have something to do with me calling it statically? Do I need to revert to using self:: instead or should I stop calling it statically?
Using $this only works for member methods. I.e. you can only use it from an instance of the class and I believe also only to access instance members.
To access static members, you should use self::.
Not familiar with the Yii framework, but what someone does is to use static methods as factory methods. Maybe that's what you're confused by. The static method then creates a new instance of itself and returns it, and inside that instance you can of course use $this, but not in the static method. Static methods have no $this.

Meaning of #method && #Command annotation in aws php sdk

I was looking into the codebase of Amazon AWS PHP sdk and found that, several methods in comments are declared as #command such as the one below(link: https://github.com/aws/aws-sdk-php/blob/master/src/Aws/S3/S3Client.php):
* #method Model getObject(array $args = array()) {#command S3 GetObject}
Can anyone please explain how this actually works? I want to know the actual working mechanism inside 'getObject' method call. Thanks in advance.
Methods such as getObject() are dynamic methods instead of static methods. The real call is getCommand('GetObject'), but the class's __call() method handles the real work.
Every service in the SDK has a Model Definition. The getCommand() method takes the inputs and maps them to the Model Definition and makes the right request.
So, think of getObject() as a convenience method, that is constructed dynamically instead of statically.

Categories