Objects in PHP

By: Andi, Stig and Derick Emailed: 1604 times Printed: 2078 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The main difference in OOP as opposed to functional programming is that the data and code are bundled together into one entity, which is known as an object. Object-oriented applications are usually split up into a number of objects that interact with each other. Each object is usually an entity of the problem, which is self-contained and has a bunch of properties and methods. The properties are the object’s data, which basically means the variables that belong to the object. The methods —if you are coming from a functional background —are basically the functions that the object supports. Going one step further, the functionality that is intended for other objects to be accessed and used during interaction is called an object’s interface.

A class is a template for an object and describes what methods and properties an object of this type will have. In this example, the class represents a person. For each person in your application, you can make a separate instance of this class that represents the person’s information. For example, if two people in our application are called Joe and Judy, we would create two separate instances of this class and would call the setName() method of each with their names to initialize the variable holding the person’s name, $name. The methods and members that other interacting objects may use are a class’s contract. In this example, the person’s contracts to the outside world are the two set and get methods, setName() and getName().

The following PHP code defines the class, creates two instances of it, sets the name of each instance appropriately, and prints the names:

class Person {

private $name;

function setName($name)

{

$this->name = $name;

}

function getName()

{

return $this->name;

}

};

$judy = new Person();
$judy->setName("Judy");

$joe = new Person();
$joe->setName("Joe");

print $judy->getName() . "\n";
print $joe->getName(). "\n";

 


PHP Home | All PHP Tutorials | Latest PHP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Andi, Stig and Derick
Execution Lifetime of a PHP script
preg_split() and explode() in PHP
preg_replace() and preg_replace_callback() in PHP
preg_match(), function preg_match_all(), preg_grep() in PHP
tmpfile() in PHP
Renaming and Removing Files in PHP
Locking files in PHP
File Handling in PHP
Handling BLOB in PHP and MySQL
Using Sessions in PHP
Using Cookies in PHP
Using PEAR::Crypt_HMAC in PHP
Using HMAC Verification in PHP
Input Validation in PHP
__autoload() METHOD in PHP

More Tutorials in PHP
PHP code to import from CSV file to MySQL
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
Convert XML to CSV in PHP
Password must include both numeric and alphabetic characters - Magento
PHP file upload (Large Files)
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Error: Length parameter must be greater than 0
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Handling file locks in PHP
HTML table output using Nested for loops in PHP
Count occurrences of a character in a String in PHP

More Latest News
Most Viewed Articles (in PHP )
isset() function in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Reading word by word from a file in PHP
Using Text file as database in PHP
Function to return number of digits of an integer in PHP
Count occurrences of a character in a String in PHP
Error: Length parameter must be greater than 0
Resume or Pause File Uploads in PHP
Different versions of PHP - History and evolution of PHP
Variables in PHP
superglobals in PHP
Here-docs (<<<) in PHP
The new keyword and constructors in PHP
Static Methods in PHP
__autoload() METHOD in PHP
Most Emailed Articles (in PHP)
preg_split() and explode() in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8
Where does the PHP run?
Password must include both numeric and alphabetic characters - Magento
PHP code to import from CSV file to MySQL
Convert XML to CSV in PHP
Handling BLOB in PHP and MySQL
Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......
Handling file locks in PHP
PHP file upload (Large Files)
PHP code to write to a CSV file from MySQL query
PHP code to write to a CSV file for Microsoft Applications
History and origin of PHP
preg_match(), function preg_match_all(), preg_grep() in PHP
Reading word by word from a file in PHP