Buy Now
Sign in

PHP function and "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" error

riccardoc , October 15 2020, 15:26
R
riccardoc 42
October 15 2020, 15:26 #394348
Hi,
I've read other questions and replies about this error but I'm not able to find the right solution to me.
in actions>load I've added a PHP module with this function:
function test($field) {
if ($this->data("language")=="it-IT") {
echo $this->get("read_data1.db_test_it.', $field,'_it");}
else {
echo $this->get("read_data1.db_test_en.', $field,'_en");}
}
in my view I call it like:
test("MyField");
The idea was to read and print the field "read_data1.db_test_it.MyField_it" if the interface is in Italian and "read_data1.db_test_en.MyField_en" in any other case but when I load my form I have the error "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" and I really can figure out what do I have to do to have it working.
Thanks in advance for any help.
admin 30
October 16 2020, 22:56 #394362
using $this should be fine, but your code has a quotes problem, please try to fix this first ?
R
riccardoc 42
October 17 2020, 07:41 #394364
Thanks for reply Max, you are right, I made some mess with quotes but I tried this just to take it easy
function test() {
$text = $this->get("read_data1.db_test_it.ID");
echo $text;
}
then I call it like
test();
and the same errore appear, If I write
<?php
$text = $this->get("read_data1.db_test_it.ID");
echo $text;
?>
in views it works, what am I missing?
admin 30
October 17 2020, 15:24 #394370
what if you test the 2 lines of code without the PHP tags ?
R
riccardoc 42
October 17 2020, 15:45 #394371
If I add a PHP module in actions>load with those two lines (so not creating a function) I have no error but no data too, I also add some text to the echo like
echo $text,'abc';
and then I see abc and nothing more, if I copy the same code in a html module in views (with PHP tags) ID field is correctly displayed.
admin 30
October 17 2020, 22:28 #394377
I added a PHP action to the first page and used this code:
function test(){
echo 123;
}

test();
that showed 123 on the top of the form which is the expected behavior!
R
riccardoc 42
October 18 2020, 06:28 #394378
Sorry for my English Max, I couldn't explain what I did.
In Actions>load>PHP module I wrote
function test() {
$text = $this->get("read_data1.db_test_it.ID");
echo $text,'abc';
}
Then in Views>HTML module I wrote
<?php
test();
echo '<br>';
$text = $this->get("read_data1.db_test_it.ID");
echo $text,'abc';
?>
The result when I load the form is
abc
25abc
So ID wasn't read in the function but only in the view code, no error reported.
Thanks again for the support.
admin 30
October 18 2020, 13:53 #394379
the PHP is above or below the "read_data1" ?
R
riccardoc 42
October 18 2020, 13:57 #394381
 1
You are right, it was before read_data1, now I moved it below but I got again the "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" error when loading the form.
admin 30
October 20 2020, 14:41 #394413
 Answer
well, it looks like $this will not work inside a function defined in the PHP code, you will need to make the function take $this as a parameter (do not name it $this), and pass $this in the function call!
R
riccardoc 42
October 20 2020, 14:47 #394415
 1
ok, thanks Max
admin 30
October 20 2020, 15:00 #394419
No problem!