Cod-nes Posted August 10, 2009 Share Posted August 10, 2009 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;?> Link to comment Share on other sites More sharing options...
Synook Posted August 10, 2009 Share Posted August 10, 2009 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();?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.