Jump to content

Question About Classes


Recommended Posts

How do classes in Php work?I'm a complete newbie about classes. Do classes get run after they are coded (in quote 1) or do we need to define them?

<?class A {echo "Booo!";}?>
<?phpclass A {echo "Booo!";}new A;A;?>
Edited by Cod-nes
Link to comment
Share on other sites

Classes are a set of algorithms, or methods, that can be applied to certain sets of data, or fields. So you first define a class, using the class declaration, then you can create instances of them, and call their methods. (Your code for both examples is syntactically incorrect, classes are NOT like functions.) For example:

<?phpclass Liquid {	public $name;	function __constructor ($name) {		$this->name = $name;		}	function drink() {		echo "The {$this->name} was drunk";	}}$water = new Liquid("water");$water->drink();?>

Edited by Synook
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...