Object Types

December 1st, 2009

There is one function and one binary operator for determining if a variable is an object.

Is_object

is_object($aVar) – Determines if $aVar is an object. When $aVar is instantiated (created) from a class, this function returns true. Otherwise it returns false.

We need an object to work with. This primitive caveman will suffice.

File: Caveman.php

<?php

class Caveman {

	public function greet() {
		echo("Grunt.\n");
	}

}

?>

The following program tests if the variable $fred is an object.

<?php

require_once("Caveman.php");

$fred = new Caveman();

if (is_object($fred) === true) {
	echo("This is an object.\n");
}
else {
	echo("This is not an object.\n");
}

?>

Since $fred was created from a Caveman class, it is an object and is_object($fred) returns true. The if condition evaluates to true and the program outputs: This is an object.

Instanceof

The binary operator instanceof determines if $aVar is instantiated from a given class. One operand is the variable being tested and the other the name of a class or interface. Moreover, it also tests the following:

  • Determines if $aVar inherits (extends) from a given class.
  • Determines if $aVar implements a given interface.

So, if a $var was created from a given class, created from a class that extends a given class or implements a given interface instanceof evaluates to true. Otherwise it returns false.

The following example tests if $fred is created from a class named Caveman.

<?php

require_once("Caveman.php");

$fred = new Caveman();

if (($fred instanceof Caveman) === true) {
	echo("This is a caveman.\n");
}
else {
	echo("This is not an caveman.\n");
}

?>

Since $fred was assigned new Caveman(); the object is an instance of the Caveman class. The instanceof operator evaulates to true and the program outputs: This is a caveman.

To demonstrate the first bullet point when instanceof evaluates to true:

  • Determines if $aVar inherits (extends) from a given class.

We need a second class that extends from Caveman.

File: RenaissanceMan.php

<?php

require_once("Caveman.php");

class RenaissanceMan extends Caveman {

	public function greet() {
		echo("Greetings, gentleman.\n");
	}

}

?>

The variable $fred is tested if it is an instantiated from Caveman. Although $fred is created from the RenaissanceMan class, the RenaissanceMan class definition inherits from the Caveman class and instanceof operator evaluates to true.

<?php

require_once("RenaissanceMan.php");

$fred = new RenaissanceMan();

if (($fred instanceof Caveman) === true) {
	echo("This is a caveman.\n");
}
else {
	echo("This is not an caveman.\n");
}

?>

The output is This is a caveman. The boolean expression ($fred instanceof RenaissanceMan) in the example above would also evaluate to true.

Demonstrating the second bullet point:

  • Determines if $aVar implements a given interface.

We need an interface, a definition that defines a behavior.

File: Talkative.php

<?php

interface Talkative {

	public function greet();

}

?>

Adjust the Caveman class definition to indicate the all caveman can greet people.

File: Caveman.php

<?php

require_once("Talkative.php");

class Caveman implements Talkative {

	public function greet() {
		echo("Grunt.\n");
	}

}
?>

The following example tests if $fred implements the Talkative interface.

<?php

require_once("RenaissanceMan.php");

$fred = new RenaissanceMan();

if (($fred instanceof Talkative) === true) {
	echo("This creature is talkative.\n");
}
else {
	echo("This creature is quiet.\n");
}

?>

RenaissanceMan inherits any protected and public methods, properties and interfaces that Caveman may have. Caveman implements the Talkative interface and since RenaissanceMan extends Caveman, RenaissanceMan also implements the Talkative interface. The binary instanceof operator evaluates to true and the program outputs: This creature is talkative.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>