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.
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);
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.
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?
For php version 5.3. This line is working fine.
<?php echo CController::createUrl('/Reload/data')?>
But when I updated my php to 5.4. I am getting
Non-static method CController::createUrl() should not be called statically, assuming $this from incompatible context
What should I have to do to make it work in yii?
You need to access a controller object. Assuming you are working within a view you can use $this to access the current controller object:
<?php $this->createUrl('/Reload/data')?>
Within a widget you can use $this->controller:
<?php $this->controller->createUrl('/Reload/data')?>
If not, you can use Yii::app()->controller:
<?php Yii::app()->controller->createUrl('/Reload/data')?>
I can't figure how this line would work even in PHP 5.3 since you are calling a non static method in a static context. CController::createUrl() actually references $this...
Anyway, back to your main point : in Yii1 you can also manage your URLs with CApplication::createUrl(), and the Application object is available anywhere in your code.
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.