Problem statement:
I have to pass a php object from one php application to another using json. In my destination application, I am not able to access my member functions after json_decode.
Here is the sample script:
<?php
class TestScope{
private $privateVar;
function __construct(){
$this->privateVar="private";
}
function getPrivateVar(){
return $this->privateVar;
}
}
$testScope = new TestScope();
$encode = json_encode($testScope);
$decode = json_decode($encode);
print_r($decode->getPrivateVar());
?>
After executing the script, I am getting below error:
PHP Fatal error: Call to undefined method stdClass::getPrivateVar()
What is the possible solution to prevent this error?
What worked for me, was serializing and un-serializing the object(s) before putting them in cookies, sessions or databases.
I suppose it will work for this problem aswell:
http://php.net/manual/en/language.oop5.serialization.php
Related
I have 3 classes, which i serialize and save to the database. 2 of them ork just fine, i am able to retrieve the object and unserialize them without a problem. The class in question has 9 properties. I pass on an array to the class, while saving:
$customer= new Customer($customerValues);
$serializedCustomer = serialize($customer);
$auftrag = new Auftrag($auftragValues);
$serializedAuftrag = serialize($auftrag);
the Customer object looks like:
O:5:"Customer"?:{s:9:"*anrede";s:4:"Herr";s:7:"*name";s:11:"ABC";s:8:"*firma";s:11:"test";s:10:"*strasse";s:16:"teststr. 33";s:6:"*plz";s:5:"1234";s:8:"*stadt";s:12:"testcity";s:8:"*mobil";s:12:"0123456789";s:10:"*telefon";s:12:"0123456789";s:8:"*email";s:18:"test#mail.com";}
and the Auftrag object:
O:7:"Auftrag":37:{s:14:"*auftrag_typ";s:7:"Angebot";s:9:"*breite";s:3:"2.5";s:8:"*tiefe";s:3:"4.5";s:8:"*hoehe";s:4:"1.00".....
I have the following function in both classes:
public function getProperties() {
$properties = get_object_vars($this);
return $properties;
}
and i fetch the data from the database and unserialize them:
require_once($_SERVER['DOCUMENT_ROOT'] . '/php/Customer.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/php/Auftrag.php');
$res = $dbc->fetchAllResults(PDO::FETCH_ASSOC);
$custArr = unserialize($res[0]['customer']);
$auftragArr = unserialize($res[0]['auftrag']);
$customer= $custArr->getProperties();
$auftrag = $auftragArr->getProperties();
When i print: print("<pre>".print_r($customer,true)."</pre>");
i get an error: Fatal error: Uncaught Error: Call to a member function getProperties() on boolean in, but it works completely fine (i get an Array), when i print: print("<pre>".$auftrag,true)."</pre>");
When i use: print("<pre>".$res[0]['customer'],true)."</pre>"); i get the object, like above.
These two objects are near to identical, except for the properties. I noticed, there is a "?" in Customer "Customer"?:. Could that be an issue? Or what am i doing wrong here?
I found the solution by doing the following:
$toDatabse = base64_encode(serialize($data)); // Save to database
$fromDatabase = unserialize(base64_decode($data)); //Getting Save Format
while the solution from #astax was also correct at pointing out the problem. But by using base64_encode, it ensured to safely serialize the object. This prevents the data from getting corrupted since base64 converts the data to ASCII which any collation can take.
According to PHP documentation, unserialize returns false if the string is invalid. This is what happens in your case - $custArr is not an instance of the Customer class, hence the error.
If you add error_reporting(E_ALL); just before unserialize, you'll see the error message, since unserialize throws a notice in case of error. There is indeed something wrong with the serialized string.
My guess is that there are some non-ASCII symbols in the values of the object and you lose them after saving and reading from the database due to mismatching character set. If you use MySQL, this page may help - https://dev.mysql.com/doc/refman/8.0/en/charset.html
I am trying to call a function from one file to another in PHP, however, I am getting the following error:
Fatal error: Call to undefined method SQLite3::print_model_query_form()
I have two files. The first - dbfunctions.php contains the method print_model_query_form().
The second file query_models contains the following code:
include_once("functions/dbfunctions.php");
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
print $db->print_model_query_form("query_models.php");
The function looks a little like this:
function print_model_query_form($action, $current_values = 0){
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
if($current_values){
// set to previous values.
}else{
// get POST values.
}
// Code to print query form.
}
Thanks in advance for any help.
Since you only hit fatal error on 3rd line, $db should be instantiated successfully. So, the issue should be the print_model_query_form method.
Referring to PHP:SQLite3 - Manual
, there is no such built in method called print_model_query_form.
[Edit 1]
Try to use require_once instead of include_once to make sure you have included dbfunctions.php successfully.
[Edit 2]
Check If you are using PHP's built in SQLite3 class (check your php.ini for extension=php_sqlite3.dll or extension=php_sqlite3.so).
If this is the case, check your dbfunctions.php for:-
class Something
new SQLite3
function print_model_query_form
If all the above exists then you should replace your 2nd line with,
$db = new Something(..);
Note: It would be better if you can show dbfunctions.php instead or letting us make assumptions based on guessing.
I'm working with 0.9.5 and I'm doing some phpunit tests.
When I execute my second test, that invokes again the webservice, I'm getting this error:
Undefined index: _transient
/var/www/dev_folder/nusoap/nusoap.php:227
/var/www/dev_folder/nusoap/nusoap.php:7293
when
$client = new nusoap_client($this->_config->URL_Path . $webserviceWSDL, true);
is executed by a second time.
I checked nusoap.php and seems something related with globals or something static or singleton... but I don't know what can I do to solve the problem...
$GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = 9;
Need nusoap client to be unloaded or something like this? Why this global variable is failing?
Thank you.
I had the same problem. The comments seemed to indicate that the global variable was an attempt to emulate a static class variable, so I simply updated the code to actually use a static class variable in the nusoap_base class. That seemed to do the trick.
You can checkout the code here.
I'm using codeigniter for a project.
The URL is segmented like this:
example.com/admin/events
example.com/admin/events/add
I have a class called Admin which has a function called events
function events($data)
{
echo $data;
}
this will echo 'add' for the events/add page but when I try and access the events page alone i get an error
Missing argument 1 for Admin::events()
I thought that I could make two functions, one with a variable being passed and one without but apprently php doesn't allow that.
Fatal error: Cannot redeclare Admin::events()
How can I make this work?
Thanks.
Try that
function events($data = NULL)
{
echo $data;
}
I'm having a problem creating webservices through nuSOAP (although i believe my problem has nothing to do with it)
What i'm trying to do:
function loadActiveItems() {
$list = Item::loadActive();
$ret = array();
foreach ($list as $val){
//two tests to check if i really have an object and if the toDTO method is callable
echo var_dump($val);
echo is_callable(array($val, 'toDTO'));
array_push($ret, $val->toDTO());
}
unset($val);
return $ret;
}
I'm getting the following error:
Call to a member function toDTO() on a non-object
and both var_dump($val) and is_callable are returning the expected (the object and true, respectively) from what i've been seeing online, it appears i have a out of scope problem... but for some reason i don't seem to get my head around it :P
Thanks in advance
EDIT: well just check that apparently i don't understand is_callable either because i always get 1 as the result...
EDIT2: i'm using php-activerecord if that helps in any way
toDTO() may be undefined in your class Item.
Another reason may be that the method isn't public or as #Grep said` static.
This error never happens on an object that defines the method but it is static or protected/private:
Call to a member function toDTO() on a non-object
That error only happens if $val is not an object. Usually a NULL, FALSE or other scalar.
It's usually a FALSE when the object came for a db_fetch() function but the fetch or the query before it failed.
It's usually a NULL when you have an array that may have NULLs in it.
var_dump($list) and see what's in there and if there are any NULLs. Also change your foreach to have a $key and var_dump($key) as well to see which key is dumped last before the error is issued.
Okay so i figured out the problem... thanks for all the help!
I was calling toDTO of another object inside toDTO... problem was that object could be a null!
So a simple if(object==null) solved the problem!
Thanks again!