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.

User

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: 4384
  • *Latest: scoobie

  • *Total Posts: 16082
  • *Total Topics: 2443
  • *Online Today: 6
  • *Most Online: 157
(27.08.09, 03:54:53)
  • *Users: 0
  • *Guests: 2
  • *Total: 2

Author Topic: [WIP] PMx coding technique - Part II  (Read 3786 times)

0 Members and 0 Guests are viewing this topic.

Offline feline

  • CO PortaMx corp.
  • Administrator
  • *
  • Posts: 5686
  • Gender: Female
[WIP] PMx coding technique - Part II
« on: 24.03.10, 20:34:40 »
We continue with the Part II of our coding technique, the class, constructor, method and attributes.

Contructor
In object-oriented programming, a constructor in a class is a special type of subroutine called when an object is created dynamically through the keyword "new". Its purpose is to prepare the new object for use, often accepting parameters which are used by the constructor to set member variables which are required when the object is first created.
A constructor is similar to an instance method, but it differs from a method in that it never has an explicit return type, it is not inherited, and usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class.

Examples
In PHP (Version 5 and above) the constructor is a method named __construct(), which is automatically called by the keyword new after creating the object. However, constructor in PHP version 4 (and earlier) is a method in a class with the same name of the class. Because PortaMx supports PHP4, we use only PHP4 type constructor.
// the class definition
class Person
{
   var 
$name;   // a class attribute
 
   // the constructor
   
function Person($name)
   {
       
$this->name $name;
   }
 
   
// a method
   
function getName()
   {
       return 
$this->name;
   }
}


Now we can create a new instance of the class by doing follow:
// define the classname
$class 'Person';
// create the instance and call the contructor
$handle = new $class('Feline');

// now you can call the method getName() and echo the result
$result $handle->getName();
echo 
$result;   // write out "Feline"


If you use a method INSIDE a class or method, then you must call these with the keyword $this.
Example:
// the class definition
class Person
{
   var 
$name;   // a class attribute
 
   // the constructor
   
function Person($name)
   {
       
$this->setName($name);   // call the method setName
   
}

   
// method setName
   
function setName($name)
   {
       
$this->name $name;
   }
 
   
// method getName
   
function getName()
   {
       return 
$this->name;
   }
}

This have the same effect as the first example. The constructor is called on create a new instance and the constructor called the method setName($name). To test this. we create a new instance of the changed class:
// define the classname
$class 'Person';
// create the instance and call the constructor
$handle = new $class('Feline');

// call the method getName() and echo the result
$result $handle->getName();
echo 
$result;   // write out "Feline"


Because we have now a method setName, we can change the name attribute:
// change the attribute $name
$handle->setName('PortaMx');

// call the method getName() and echo the result
$result $handle->getName();
echo 
$result;   // write out "PortaMx"


To be continued ...

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