...

View Full Version : Understanding Classes



ptmuldoon
12-24-2007, 07:28 PM
Can someone possibly explain how php classes work? I'm looking to add a function to an existing php class. Should the function be ran automatically when the class file is called?

Or do you need to include the class file, and then run the function separately?

Mwnciau
12-24-2007, 08:00 PM
Functions that are run when the class is created are called contructors. These can accept parameters by adding parentheses after the class name. If you just want to add functions without them having run automatically, it's pretty much the same as normal but within the class.

PHP 4

// Constructor
class constructClass
{
var $variable;

// The constructor is the function with the same name as the class
function constructClass ( $parameter )
{
$this -> variable = $parameter;
}
}

$class = new constructClass( 'hi' );

echo $class -> variable;
/* The -> allows you to access variables and functions inside a class
This example we are accessing the variable $variable inside the $class instance of the class */

PHP 5 is slightly different

PHP 5

class constructClass
{
public $variable; // Here the 'public' keyword is used as var is decrepit,
// public means you can access the information from outside the class

// The constructor is the function with the name __construct
public function __construct ( $parameter )
{
$this -> variable = $parameter;
}
}

echo $class -> variable;

ptmuldoon
12-24-2007, 08:45 PM
That confused me a little bit. But I'm actually working php 5, and have a class that includes function __construct() at the beginning of the class.

Do I understand correctly that if I want to have the function ran automatically that I need to add a constructor within the function __construct() ?

Mwnciau
12-24-2007, 09:00 PM
Anything within the __construct function will be run automatically when you create a new instance of the class.

srule_
12-24-2007, 09:19 PM
I just started learning OOP (like 3hrs ago lol) so correct me if I am wrong, But I am pretty sure you are only allowed ONE construct function per class in php OOP.

also the construct class could be either : __contstuct(), Or it could share the same name as the class, ex my class name = test, so constructor function is test(). Using __constuct is new to PHP 5 and is the more favorable way of doing things.

oesxyl
12-24-2007, 09:54 PM
Can someone possibly explain how php classes work? I'm looking to add a function to an existing php class. Should the function be ran automatically when the class file is called?

Or do you need to include the class file, and then run the function separately?

There are two kind of function:
- function what use objects and don't belong to them
- a function what belong to a class, called method
both could be called inside constructor.

You can modify the source of the class to do what you want, but this is
bad practice, usualy you must extend the class to fit your need.

I think that could be more easy to help if you give an example about what you want to do.

best regards and happy holidays.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum