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!
Related
I've got this line in code in module CMap.php which is part of the yii framework which should count the number of elements in array _d.
return count($this->_d);
Howver, if _d is an array[0] it raises the error:
Trying to obtain property of non object
Using PHP v5.3.3 (x86) which is the same version as installed on our webserver.
Any idea why this is happening? Thanks.
EDIT:
Changed the code to this:
try {
return null;
if ($this->_d==null)
return null;
else {
if(isset($this->_d[$key]))
return $this->_d[$key];
else
return null;
}
}
catch (Exception $e) {
return null;
}
And it still throws an errors on the first return null; line without entering the catch() block.
Let's break down the error message:
Trying to obtain property
A "property" is a field of some object, and "property access" is what you do with the -> operator. In your case, $this->_d.
of non object
In the expression $this->_d, we would say "_d is a property of $this". So "of a non-object" means the thing on the left of the -> operator is not an object.
So assuming the code you've posted is accurate, and not anonymized to the point where it's hiding the real error, your problem is that $this is not an object in that part of the code.
Note that this contradicts your claim that $this->_d is an empty array, because if $this is not an object, then $this->_d does not exist. So I strongly suspect that you're looking in the wrong place, or you've tried to simplify the description but missed out crucial details.
Ok, I'm going to close this thread off now.
The code posted in my OP wasn't buggy, and wasn't causing the error.
Complete red herring. What is happening is that the code running after this method (the code which calls this method) is throwing the error, but for some reason that code doesn't show up in my debugger. I can't step through it.
The first I see of any debugging is when a breakpoint in the ErrorHandler gets hit.
I'll raise a separate Question on how/why the YII framework is doing that.
Thanks for you comments guys.
I am trying to setup OData connection to MySQL database .. when I run the MySQLConnector.php , I am getting the above error.. can someone please direct me towards a solution.
The statement that gives the error is:
public function getEntityTypeName($entity)
{
return \Inflector::singularize($entity);
}
Following is the code in Inflector.php:
function singularize($word) {
$_this =& Inflector::getInstance();
if (isset($_this->_singularized[$word])) {
return $_this->_singularized[$word];
}
Please let me know if you would need any further information. Thanks in advance.
The short answer is: you need to update both of them. It looks like you have an older Inflector which is relying on deprecated PHP behaviour, and it's likely that your MySQLConnector.php is also old. If you don't update, you'll likely hit further problems.
In this case, PHP is complaining that you're using a static call to a method which is missing the "static" keyword. It's very likely that this message is a warning not an error, so it probably isn't causing whatever end problem you're experiencing. If you really want to address this message, you can just write static public function singularize($word) { instead, but like I said you will have more problems.
I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit
I'm Not too sure on what this error is, from looking around, it must be something to do with the database declarations. I'm trying to make a drop down box on my Widget, by selecting different fields of the database, different masks will be selected and will allow for different widgets to be made on later pages.
The part of my code where i think the error is, is:
$this->build("p4a_db_source", "login")
->setTable("meetingrooms")
->addJoin("login.meetingrooms",
"login.meetingrooms.MeetingRoom = login.meetingrooms.MeetingRoom",
array('position'=>'Ident'))
->addOrder("position")
->load();
$this->setSource($this->login);
$this->firstRow();
$this->build('p4a_field','location')
->setSource('login')
->setLabel('location')
->setValue('Please Select...')
->setType('select')
->setWidth(60);
$this->weight->label->setWidth(60);
I know its a similar question to my previous one, but its a different code entirely, but this one should be much easier to fix.
Thanks for the help.
The Stacktrace (Fatal error: Call to a member function getPk() on a non-object in C:\xampp\htdocs\p4a\p4a\objects\widgets\field.php on line 468) isn't indicating the line at which the error is occurring so i'm unsure where exactly the problem is originating from,
The rest of the code (including previous) is:
class main_dashboard_mask extends P4A_Base_Mask
{
public function __construct()
{
parent::__construct();
$this->setTitle("Dashboard");
$this->build('p4a_field','MeetingRooms');
$this->MeetingRooms->setLabel("This is the meeting room label");
$this->build('p4a_button', 'continue')
->setLabel('Continue?')
->implement("onclick", $this, "change");
$this->build('p4a_field','booking')
->setlabel('Viewing?')
->setType('checkbox')
->setValue(true);
$this->booking->label->setTooltip('If you are booking a meeting room, check this box');
$this->build("p4a_db_source", "login")
->setTable("meetingrooms")
->addJoin("login.meetingrooms",
"login.meetingrooms.MeetingRoom = login.meetingrooms.MeetingRoom",
array('position'=>'Ident'))
->addOrder("position")
->load();
$this->setSource($this->login);
$this->firstRow();
$this->build('p4a_field','location')
->setSource('login')
->setLabel('location')
->setValue('Please Select...')
->setType('select')
->setWidth(60);
$this->weight->label->setWidth(60);
$this->Meetingrooms();
}
private function Meetingrooms()
{
$this->build('P4A_fieldset', 'widgetframe')
->anchor($this->location)
->anchorLeft($this->booking)
->anchorLeft($this->continue)
->setLabel('Meeting Room Bookings');
}
}
I think you are not getting the object. That is why it's giving error of non-object.
Just print the object on which you are calling the method getPk(). If it is valid object then
only call that method.
i got it, sorry i was looking in the right place but didn't see where i was wrong...
where before the code was ->
$this->setSource($this->login);
$this->firstRow();
$this->build('p4a_field','location')
->setSource('login') // <- this is the error(the Pk variable hasn't been set here)
->setLabel('location')
->setValue('Please Select...')
->setType('select')
->setWidth(60);
$this->weight->label->setWidth(60);
$this->Meetingrooms();
the fix is ->
->setSource($this->login)
Thanks for the assistance =]
Do you have a full stacktrace ? What line of code exactly generates this error ?
Anyway, you have to locate the code where $object->getPk() is called. The error means that you're trying to use a function ->getPk() on an $object that is null ..
I'm having an issue with PHP as it keeps throwing the Exception mention in the title.
It fails on the following line:
$item->getDescription();
I understand what the error should mean ($item is null). However, $item is not null.
The scenario is as follows:
This is a script that syncs products from a supplier to a store. For that purpose, I have created my own class (SimpleProduct). This class has a getDescription() function.
The problem is that the data I'm receiving tend to have a lot of garbage, like items that haven't been filled in yet. The script should skip these items and keep on iterating across the rest of the products.
This fatal error kills the entire script.
I've already tried implementind safeguards to prevent this from happening, but it still occurs constantly. Here's the current code (some snippets removed as they arent pertinent to the currect case).
//This is part of a class that performs the sync
public function syncProduct($item) {
if(empty($item)) { return "Not a product"; }
else { var_dump($item) }
$foo = $item->getDescription();
}
When checking the var_dump result, I get an object with some values filled in. Seeing as it is of the correct type (SimpleProduct) and it is not empty/null, I would suspect this error to stop occurring, but it still does.
Also note that several product syncs have already occurred without any errors before this one pops up, so I know the code is valid. Somehow, this specific case slips past my null-checks.
Is my null-check faulty?
How can an error for a non-object be thrown when the object in question does exist?
Instead of checking whether if the variable is empty, why not check whether if it's an instance of SimpleProduct?
if ($item instanceof SimpleProduct)
{
}
http://php.net/manual/en/language.operators.type.php
Surely the object is still not available in the context of the function syncProduct.
Try to do a var_dump($item) to confirm its there and execute it within the else part of the code to ensure its not empty.
To check if $item is an object You can use is_object()
Your null check is not preventing the object to be used even if it is null contains non-objects.
Use this:
public function syncProduct($item) {
var_dump($item);
if($item InstanceOf SimpleProduct) {
$foo = $item->getDescription();
}
return "Not a product";
}
I stand corrected! I didn't notice the return statement. The other case for this to occur would be if the value from $item would be non-empty but not a product either - most likely a scalar or array because using objects as other types of objects issue a different error regarding methods not being found.
I also run into a similar problem where after running this:
$user = DB::getInstance()->action($action="SELECT * ", 'users');
Then checking whether $user is an instance of DB, I found that it wasn't. I then decided separate it as follows:
$user = DB::getInstance();
$user->action($action="SELECT * ", 'users');
After doing this and using instanceof() method it shows that it is now an instance and the fatal call to member function error disappears.