Author Topic: OO Programming Data Members and Functions (2 of 7)  (Read 255 times)

0 Members and 1 Guest are viewing this topic.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 368
  • Karma: +1/-0
    • View Profile
OO Programming Data Members and Functions (2 of 7)
« on: August 31, 2008, 11:08:12 PM »
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.

Techronnati | where technology never sleeps

OO Programming Data Members and Functions (2 of 7)
« on: August 31, 2008, 11:08:12 PM »

Mountain View


Related Topics

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.