Create a simple PHP object-PHP Tutorial-php Tutorial

Source: Internet
Author: User
Create a simple PHP object .? Php defines a digital Tyrannosaurus class classDigimon {var $ name; var $ hitPoint; var $ attack; var $ defense; functionattack () {echoattack;} creates an agu $ agunewDigimo object from the class Digimon
 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.

Http://www.bkjia.com/PHPjc/752581.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752581.htmlTechArticle? Php // defines a class Digimon {var $ name; var $ hitPoint; var $ attack; var $ defense; function attack () {echo "attack ";}} // create an object agu $ agu = new Digimo from the class Digimon...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.