Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON

When you ask your program to "print out an object", you're really looking to convert a data structure into a stream of characters, and it's not really obvious what you're looking to do.

If you try to print out an object in PHP:
  print ($result . "\n");
where the $result variable contains an instance of an object, you'll get a message like:
  Catchable fatal error: Object of class station could not be converted
  to string in /Users/grahamellis/oop/oop5.php on line 108


How do you avoid your program failing like this? There are a number of options.

1. You can run a method on your object that will convert it to a string:
  print ($result->describe() . "\n");

2. You can serialize your object using the built in PHP serialise function. This will convert the object to s atring in predetermined format, so that you can unserialize it later ... and you'll get a string which you can save to file, print out, transmit to another computer, etc.

3. You can (from PHP 5.2) serialise the object with json_encode. "JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate." ... [JSON intro ans spec]

Here's an example of JSON encoding:

  print (json_encode($result) . "\n");

  {"info":"5268\tBTN\tBN1 3XP\tTQ 309050\t50.8289532182\t-0.141225193\tBrighton
  \t11295080\t11854512\t12853442\t13474555\t13806628\t13741582","cached":1,
  "fields":["5268","BTN","BN1 3XP","TQ 309050","50.8289532182","-0.141225193",
  "Brighton","11295080","11854512","12853442","13474555","13806628","13741582"]}


4. You can also trap your fatal error in PHP. Prior to any code that may fail and need to be caught, set up an error handler:

  set_error_handler('cat_chit');

and that will divert errors from the default handler to your function cat_chit. Here's an example function:

  function cat_chit($errno, $errstr, $errfile, $errline) {
    if ( E_RECOVERABLE_ERROR===$errno ) {
      print ("\n\ncaught a normlly fatal error\n");
      print ("Error number: $errno\n");
      print ("Error string: $errstr\n");
      print ("Error file: $errfile\n");
      print ("Error line: $errline\n");
      return true;
    }
    return false;
  }


The true return tells PHP that we're done with handling the error and it may proceed; the false return indicates that the error has not been (fully) handled by your handler and that the default one should also be called.

So .... 4 possible solutions - which is "best"? It's for you to decide the context. Overriding the error handler makes me worry - you're may be just masking a coding issue. Running a method (which you may need to write) on the object to present it in human-readable form is best for a production program. Serializing or json_encoding is best if you want to store the object, send it to another system, or print it out for analysis and debug purposes.

On yesterday's Object Oriented PHP Course, I developed an example during the day that showed many OO related features of PHP ... and some other features too, including the JSON and error_handler examples above. You can see then in full source code context [here].
(written 2011-03-22)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H999 - Additional PHP Material
  [54] PHP and natural sorting - (2004-09-19)
  [239] What and why for the epoch - (2005-03-08)
  [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
  [322] More maps - (2005-05-23)
  [337] the array returned by preg_match_all - (2005-06-06)
  [372] Time calculation in PHP - (2005-07-08)
  [468] Stand alone PHP programs - (2005-10-18)
  [483] Double Dollars in PHP - (2005-11-02)
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [665] PHP Image viewing application - (2006-04-01)
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [789] Hot answers in PHP - (2006-07-02)
  [806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)
  [822] PHP - a team member leaves - (2006-08-04)
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [917] Syntax checking in PHP - (2006-11-07)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [1010] Dates, times, clickable diarys in PHP - (2006-12-28)
  [1020] Parallel processing in PHP - (2007-01-03)
  [1053] Sorting people by name in PHP - (2007-01-26)
  [1104] Drawing dynamic graphs in PHP - (2007-03-09)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1270] PHP Standalone - keyboard to screen - (2007-07-18)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1519] Flipping images on your web page - (2008-01-26)
  [1623] PHP Techniques - a workshop - (2008-04-26)
  [2073] Extra PHP Examples - (2009-03-09)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [2684] Exception handling in PHP - (2010-03-18)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [4655] Image indexer / thumbnail display scripts in PHP - (2016-02-25)

H117 - Security in PHP
  [345] Spotting a denial of service attack - (2005-06-12)
  [426] Robust checking of data entered by users - (2005-08-27)
  [920] A lion in a cage - PHP - (2006-11-10)
  [947] What is an SQL injection attack? - (2006-11-27)
  [1052] Learning to write secure, maintainable PHP - (2007-01-25)
  [1086] Injection attacks - safeguard your PHP scripts - (2007-02-20)
  [1323] Easy handling of errors in PHP - (2007-08-27)
  [1387] Error logging to file not browser in PHP - (2007-10-11)
  [1396] Using PHP to upload images / Store on MySQL database - security questions - (2007-10-19)
  [1482] A story about benchmarking PHP - (2007-12-23)
  [1542] Are nasty programs looking for security holes on your server? - (2008-02-17)
  [1679] PHP - Sanitised application principles for security and useability - (2008-06-16)
  [1694] Defensive coding techniques in PHP? - (2008-07-02)
  [1747] Who is watching you? - (2008-08-10)
  [1779] Injection Attacks - avoiding them in your PHP - (2008-08-31)
  [2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
  [2628] An example of an injection attack using Javascript - (2010-02-08)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [2939] Protecting your images from use out of context - (2010-08-29)
  [3698] How to stop forms on other sites submitting to your scripts - (2012-04-15)
  [3747] An easy way to comply with the new cookie law if your site is well designed - (2012-06-02)
  [3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
  [4642] A small teaching program - demonstration of principles only - (2016-02-08)

H108 - Objects in PHP
  [67] Object Oriented Programming in PHP - (2004-09-29)
  [124] PHP v Java - (2004-11-20)
  [205] PHP5 lets you say no - (2005-02-07)
  [343] Should I use structured or object oriented? - (2005-06-10)
  [421] Don't repeat code - use loops or functions - (2005-08-21)
  [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [720] Planning a hotel refurb - an example of a Gant chart in PHP - (2006-05-14)
  [836] Build on what you already have with OO - (2006-08-17)
  [1027] Cue the music, I'm happy. - (2007-01-09)
  [1153] Object Oriented Model - a summary of changes from PHP4 to PHP5 - (2007-04-18)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1535] OO PHP demonstration - comparing objects and more - (2008-02-08)
  [1682] Accounts in PHP - an OO demo - (2008-06-19)
  [1819] Calling base class constructors - (2008-10-03)
  [1820] Sorting objects in PHP - (2008-10-04)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2160] PHP - getclass v instanceof - (2009-05-07)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2171] Cleaning up redundant objects - (2009-05-11)
  [2172] PHP4 v PHP5 - Object Model Difference - (2009-05-11)
  [2380] Object Oriented programming - a practical design example - (2009-08-27)
  [2434] Abstract classes, Interfaces, PHP and Java - (2009-10-03)
  [2435] Serialization - storing and reloading objects - (2009-10-04)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [2641] Object Oriented Programming in PHP - (2010-02-19)
  [2680] Static class members in PHP - a documented example - (2010-03-16)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2741] What is a factory? - (2010-04-26)
  [2774] PHP - Object Oriented Design in use - (2010-05-21)
  [2921] Does copying a variable duplicate the contents? - (2010-08-14)
  [2922] Getting the OO design write - with PHP a example - (2010-08-14)
  [3142] Private and Public - and things between - (2011-01-22)
  [3211] Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion - (2011-03-24)
  [3607] Designing your application - using UML techniques - (2012-02-11)
  [3608] Design Patterns - what are they? Why use them? - (2012-02-12)
  [3609] How do classes relate to each other? Associated Classes - (2012-02-12)
  [3840] Autoload in PHP - (2012-08-17)
  [3841] Copying, duplicating, cloning an object in PHP - (2012-08-18)
  [3843] Caching Design Patterns - (2012-08-20)
  [3953] Objects in PHP - Revision - (2012-12-16)
  [4057] stdClass in PHP - using an object rather than an associative array - (2013-04-02)
  [4073] Learning about Object Orientation in PHP - a new set of examples - (2013-04-28)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
  [4626] Singleton design pattern - examples and uses - (2016-01-20)
  [4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)
  [4628] Associative objects - one object within another. - (2016-01-20)


Back to
Seeing Electricity Pylons near Melksham
Previous and next
or
Horse's mouth home
Forward to
Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion
Some other Articles
Melksham Campus - any last minute inputs from Melksham businesses?
Should I use Open Source or Commercial software?
Well House Manor - a home from home for the business and leisure guest in Melksham
Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON
Seeing Electricity Pylons near Melksham
Links for social media, microblogs and business networking
Lambing at Lackham
Wiltshire College / Lackham College - the animals
Open Source (Public) courses - PHP, Tcl, SQL, Python, C and C++ in Melksham, Wiltshire, UK
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3210_Cat ... -JSON.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb