Object datatype in PHP
How to define an Object variable and assign value?
Explanation
PHP also supports composite data types, such as arrays and objects. Composite data types represent a collection of data, rather than a single value. An object is a compound data type that can contain any number of variables and functions. To initialize an object, you can use the new statement to initialize object to a variable.
<?php
class foo
{
function getdata()
{
$str="Hello World";
$this->str = $str;
}
function display()
{
?>
<?=$this->str;?>
<?
}
};
$bar = new foo;
$bar->getdata();
$bar->display();
?>
This code creates an object variable $bar for class foo using the new operator. The foo object also defines a function, known as a method, called getdata( ) and display(). Then it sets a variable called str within the function getdata(). This method uses the special-purpose $this variable to change the value of the str property within that object.
The function getdata() is called first by using the object $bar. Here value of $str is assigned to object variable. By calling the function display(), the value assigned to the object variable is returned. PHP3 OOPS concepts such as inheritance and constructors, but does not support multiple inheritance, data protection (or encapsulation), and destructors.
Another way of looking at objects is that they allow you to create your own data types. You define the data type in the object class, and then you use the data type in instances of that class.
There is also an is_object() function to test whether a variable is an object instance.