print

PHP Logo

PHP Question: Print Object

28th April 2011 - 3 minutes read time

Question

The following code was executed.

<?php
class MyClass {
  private $foo = "bar";
}

$myObject = new MyClass();
echo $myObject;

Which produced the following error.

Catchable fatal error: Object of class MyClass could not be converted to string in test.php on line 7

Call Stack:
    0.0011     323736   1. {main}() /test.php:0

How can the code be simply changed to prevent this error and produce some form of result?

WordPress Logo

Displaying Wordpress Authors

26th February 2009 - 5 minutes read time

Wordpress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn't be confused with the the_author_link() function that prints out the link in the author's profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

PHP Logo

Highlight Code In PHP

12th January 2008 - 2 minutes read time

When printing off source code there is a handy function that will parse the code and produce nice looking syntax highlighted code. There are actually two functions you can use. The highlight_string() function takes a string as a parameter and will print the highlighted code. The highlight_file() function takes a file name as a parameter, the contents of which are printed off with highlighted syntax. For now I will concentrate on the highlight_string() function, but the output of these two functions is the same.

To use the highlight_string() function just pass it a string. The following code:

highlight_string('<?php 
function checkslash($slashes_string){
  if (get_magic_quotes_gpc()==1) {
    return $slashes_string;
  }else{
    return addslashes($slashes_string);
  };
};
?>');

Will produce the following output: