PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Object datatype in PHP


Tutorials »Php »

Topic

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.





Other Links

web hosting