Language: 
To browser these website, it's necessary to store cookies on your computer.
The cookies contain no personal information, they are required for program control.
  the storage of cookies while browsing this website, on Login and Register.
Welcome, Guest. Please login or register.
Did you miss your activation email?


Login with username, password and session length

Select language:

Community



Donate

Donate for PortaMx !
Your donation is safe and helps support the issues and causes you care most about.

Stats

  • *Total Members: 4319
  • *Latest: thygrim

  • *Total Posts: 15776
  • *Total Topics: 2382
  • *Online Today: 3
  • *Most Online: 157
(27.08.09, 03:54:53)
  • *Users: 0
  • *Guests: 1
  • *Total: 1

Author Topic: [WIP] PMx coding technique - Part III  (Read 4558 times)

0 Members and 0 Guests are viewing this topic.

Offline feline

  • CO PortaMx corp.
  • Administrator
  • *
  • Posts: 5584
  • Gender: Female
[WIP] PMx coding technique - Part III
« on: 24.03.10, 21:50:00 »
We continue with the Part III of our coding technique, the Inheritance or "Subclasses".

In lesson I we have learned, what is Inheritance and what this can do for you. Because PortaMx use these technique , we want to get examples of "Subclasses".
First we have to define a "root" class like this:
// the class definition
class Cat
{
   var 
$name;   // class attribute
 
   // the constructor
   
function Cat($name)
   {
       
$this->name $name;
   }
 
   
// a method
   
function showCat()
   {
       echo 
'This cat have the name '$this->name;
   }
}


Now we create a "Subclass" from the root class:
// the class definition
class Cats extends Cat
{
   var 
$color;   // class attribute

   // the constructor
   
function Cats($name$color)
   {
       
$this->Cat($name);   // call the "root" constructor
       
$this->color $color;
   }

   
// a method "overload"
   
function showCat()
   {
       echo 
'This '$this->color .' cat have the name '$this->name;
   }
}


Now we create a instance from the "root class" Cat:
$class 'Cat';
$rootclass = new $class('Finja');
$rootclass->showCat();

This will display the string This cat have the name Finja.

Now we create a instance from the "Subclass" Cats:
$class 'Cats';
$handle = new $class('Lucy''brown-white');
$handle->showCat();

This will display the string This brown-white cat have the name Lucy.

We create a second instance from the "Subclass" Cats:
$class 'Cats';
$handle2 = new $class('Jenny''red tabby');
$handle2->showCat();

This will display the string This red tabby cat have the name Jenny .

In this example you see, that the subclass have only a attribute ($color) they are not available in the root class.
Furthermore we use here a other OOP functionality .. the method overload (or overwrite). This is a specially technique to expand or modify a existing method in a parent class.
Also you have to note, that a "Subclass" do not MUST have a constructor. If no constructor defined, the parent constructor is called automatically.

To be continued ...

Fel
Many are stubborn in relation to the path, a few in relation to the target.

Offline IchBin

  • Newbie
  • *
  • Posts: 1
  • Gender: Male
Re: [WIP] PMx coding technique - Part III
« Reply #1 on: 18.06.10, 20:36:23 »
Exellent tutorials Feline! But I think you got some things mixed up in this one. I do not know OOP fluently yet, but I like to think I have a basic understanding. :)


Now we create a instance from the "root class" Cats:
$class 'Cats';
$rootclass = new $class('Finja');
$rootclass->showCat();
This will display the string This cat have the name Finja.


Root class is actually "Cat" because "Cats" extends the root class. Which would mean the constructor needs the proper signature. I'm assuming you meant to do this instead?
Now we create a instance from the "root class" Cat:
$class 'Cat';
$rootclass = new $class('Finja');
$rootclass->showCat();
This will display the string This cat have the name Finja.


This section looks wrong as well:

Now we create a instance from the 
"Subclass" Cat:
$class 'Cat';
$handle = new $class('Lucy''brown-white');
$handle->showCat();
This will display the string This brown-white cat have the name Lucy.


Should be this:
Now we create a instance from the "Subclass" Cats:
$class 'Cats';
$handle = new $class('Lucy''brown-white');
$handle->showCat();
This will display the string This brown-white cat have the name Lucy.


It looks to me like you fell victim of what many software developers do when they use similar class names. ;) The last section is referencing root class instead of sub-class as well.