<? Php // defines a class Digimon {var $ name; var $ hitPoint; var $ attack; var $ defense; function attack () {echo "attack ";}} // create an object agu $ agu = new Digimon () from the class Digimon; // assign $ agu to the object agu-> name = "agu "; $ agu-> hitPoint = 50; $ agu-> attack = "12"; $ agu-> defense = "18 "; // The field echo $ agu-> name of the Access Object agu. "The lifecycle is ". $ agu-> hitPoint."
"; Echo $ agu-> name." attack power is ". $ agu-> attack ."
"; Echo $ agu-> name.": ". $ agu-> defense ."
"; // Method echo $ agu-> name." The current action is "; echo $ agu-> attack ();?>
Program output:
The lifecycle of the yagu beast is 50.
The yagu attack capability is 12.
The defensive capability of the yagu beast is 18.
The current yagu action is attack.
The Code demonstrates how to create a class, how to create objects from the class, how to access object fields, and how to access objects.
PHP's OOP programming is similar to Java's. If there is a basis for Java programming, PHP's OOP programming is quite easy to use.