Home arrow PHP arrow Page 2 - Object Oriented Programming in PHP

Object Oriented Programming in PHP

This article introduces Object Oriented Programming (OOP) in PHP. Luis shows you how to code less and better by using some OOP concepts and PHP tricks.

Author Info:
By: Luis Argerich
Rating: 4 stars4 stars4 stars4 stars4 stars / 356
January 13, 2003
  1. · Object Oriented Programming in PHP
  2. · Data Members and Functions
  3. · Constructors
  4. · Overloading
  5. · OOP Programming in PHP
  6. · Copying and Cloning
  7. · Conclusion

print this article

Object Oriented Programming in PHP - Data Members and Functions
(Page 2 of 7 )

Data members are defined in PHP using a "var" declaration inside the class and they have no type until they are assigned a value. A data member might be an integer, an array, an associative array or even an object.

Methods are defined as functions inside the class, to access data members inside the methods you have to use $this->name, otherwise the variable is local to the method.

You create an object using the new operator:

$obj=new Something;
Then you can use member functions like:

The setX member function assigns 5 to the x data member in the object obj (not in the class), then getX returns its value; 5 in this case.

You can access the data members from the object reference using for example: $obj->x=6. However, this is not a very good OOP practice.

I encourage you to set data members by defining methods to set them and access the data members by using retrieving methods.

You'll be a good OOP programmer if you consider data members inaccessible and only use methods from the object handler. Unfortunately PHP doesn't have a way to declare a data member private so bad code is allowed.


Inheritance is easy in PHP using the extends keyword:


class Another extends Something {
var $y;
function setY($v) {
// Methods start in lowercase then use uppercase initials to
// separate words in the method name example getValueOfArea()

function getY() {
return $this->y;


Objects of the class "Another" now have all the data members and methods of the parent class (Something) plus its own data members and methods.

You can use:

$obj2=new Something;

Multiple-inheritance is not supported so you can't make a class extend two or more different classes.

You can override a method in the derived class by redefining it. If we redefine getX in "Another" we can no longer access method getX in "Something".

If you declare a data member in a derived class with the same name as a data member in a Base class the derived data member "hides" the base class data member when you access it.
blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials