Cod-nes Posted August 10, 2009 Report Share Posted August 10, 2009 (edited) 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 August 10, 2009 by Cod-nes Link to comment Share on other sites More sharing options...
Synook Posted August 10, 2009 Report Share Posted August 10, 2009 (edited) 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 August 10, 2009 by Synook Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now