Jump to content
xisto Community
BCD

Working And Handling Non-Existent Objects

Recommended Posts

This article talks about a particular issue/error in PHP:

Fatal error: Call to a member function X() on a non-object in htdocs/website/awesome-script.php on line #
Notice: Trying to get property of non-object in /htdocs/somewebsite/d/script.hp on line #
To beginners:

In an object, a function is called as a method and a data variable is called as a property. In PHP 5, objects are passed by reference.

 

The above mentioned errors occur when a statement tries to access a method/property which doesn't exist. This issue can arise in many situations, when assigning an object method to a variable. This has been a common issue. I have seen a drupal user reporting this issue in the bug tracker of a drupal module. On Deviant art website:

Posted Image..etc

 

Consider this specific example of working with objects in PHP DOM, as this involves a lot of objects being created based on raw html data.

 

$temp = $html->find('div', 4)->innertext;

This statement uses a DOM object '$html' and finds the the fourth div in it, gets the text inside the tags and assigns it to the variable $temp. There are several issues need to be taken care of while doing this.

1) Checking whether the object has been created in the first place. This can happen when... a function which gets the raw html from a non-existent url, or which results in empty html page.

2) Even if the page exists, the specific tags, in this example the fourth div might not exist.

 

So these situations need to be taken care of before executing that statement.

1) The first issue can be taken care of by a) checking for the response status codes, and allowing only the page issuing 200 status code. B) Using filesize() to check if it results in zero size page.

2) This issue can get complicated based on the level of complexity the DOM being worked upon. This is mainly dependent on the the website, if it has dynamic changing content then it becomes difficult to target and fix elements on the page.

3) When not sure of the structure of an object, using "get_class_methods" and "get_ class_ vars" gives an array of all the methods and properties present in that object.

4) When not sure if the object is indeed an object, "is_object" returns a boolean value to confirm upon.

 

There might be many other things to check upon before working with such objects. Have you come across any, or do can you think of any other possibilities to look upon? If so please do share them with us.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.