Web Develop Forum: Lesson 7 - Object Oriented (2) - Web Develop Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lesson 7 - Object Oriented (2) Covering Polymorphism, Inheritence and Overloading

#1 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Posted 16 March 2009 - 12:11 PM

I've restored Lesson 7 below - but there will be no more lessons after I've restored 7 and 8. Please view my posts elsewhere for the discussion on why - and how to contact me if you so wish.

===============================================================================

Welcome again.

Well straight into it. Overloading is the idea of having functions do the same thing, with different numbers of arguments. In compiled languages such as C++ you usually have to define multiple functions with different arguments. In PHP we dont have to do this (in fact we cant do this), so instead we tell the function a default value for arguments:

function display ($fname="default", $lname="surname") {}

so we can call it like:

display("ME");

This way - we can "overload" our functions, allowing us to have different "setters" without having to manaully set() each variable.

This brings me to another point about class design. Most of the time you want to have the member variables private, and only allow access to them through methods of the class. Often you call these "getters" and "setters":

class name {
private $variable;
public function setvar($val) {}
public function getvar() {return $this->variable; }
}

Now clearly you can see that its a basic example - but it allows your to really secure your classes.


Inheritence allows you to "inherit" parts of a parent class:

class classone {
.... stuff
}

class childclass extends classone {
parent::function();
}


So here I've declared the parent class, and then I've inherited its properties in the child class. Also in the child class I've called the function called function - but the one that belongs to the parent class - so that if you decide to override the child class method then you can still access the parent member. A child class inherits public and protected members from the parent, however private members are NOT inherited into the child. Generally, if you are designing a class for inheritence... protected is the "family friendly" private scope.

This technique becomes very useful when modeling things with similar properties. For example imagine a network. You could have a parent class Node, which contains an IP address. Then child classes could be DNSServer, Router etc. - all of which would have an IP address, but would have other properties as well.

As you can see, these techniques can become very powerful. Over all - its called polymorphism... By being about to access parents and change the way we call different objects, our code becomes much more powerful.


So to wrap up this lesson, I'll leave you with a few examples (Which will probably extend into homework)...
- Modeling an HTML form, each child class extending a parent "Element" class.
- Creating a game, where each character or object on screne inherits from a parent - containing animation functions etc.
- Easily accessing file access methods, and wrappers that are object oriented for basic I/O operations.

So hopefully this hasn't been too much to take in at once. For homework I'll be setting a few modelling things with OOP - things like the HTML form. As such it wont be code at all - but as more of a theory exercise, planning out all of the work.

Soon(ish), once weve had a chance to actually build something usable, I'll talk more about "projects" and "styles" of coding etc. - which I personally find just as important as teh code itself. I Often find myself planning a few hours coding for more hours than it takes me to code - as I work through examples of test data, makeing sure that the theory and model behind it makes it fit the requirements and is easy to debug - but more on that when it comes.

C ya!

This post has been edited by Jack: 10 August 2009 - 09:05 PM

0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users