PHP and the Like, objects (iv) loading classes and exercises

Source: Internet
Author: User

First, Load class:
1. When naming the class file, capitalize the first letter of each word, followed by the prefix. class.php
eg:Info.class.php
Define class name capitalization when writing code, define variable name lowercase
Eg:class Ren
{
Public $name;
Public $sex;
}
2. Load class:
2.1 How to find a file for a class

Current directory:./
Parent directory:. /
Subordinate directory: Directory name/
Root directory:/
If the actual PHP code inside/represents the local disk
If it is in HTML/represents the current site Directory


Include (".. /info.class.php ");//load file to page, parameter is a path (can find class file)
Include "/appserve/www/info.class.php";

Require_once "./info.class.php";//Request the target file once
Require_once ("./info.class.php");

Require ("Info.class.php");
Require "Info.class.php";

2.2 Auto-load class:

function __autoload ($classname)//In parentheses is the class name
{
Include ($classname. ". Class.php ");
}
Requirements: 1. All classes are named the same as 2. All classes must be in the same folder 3. The name of the class is best and the file name is the same
Attached: This method is generally not used


Second, examples:
1. Find the middle shadow area of the big circle and the small circle:

Process oriented:
$r 1=10;
$r 2=5;
$MJ = $r 1* $r 1*3.14-$r $r 2*3.14;
Echo $MJ;

Object-oriented:

(1.) Create a new PHP file named Yuan.class.php
Class Yuan
{
Public $r;
function __construct ($r)
{
$this->r= $r;
}
Functin Mianji ()
{
return $this->r* $this->r*3.14;
}

}
(2.) Source code:

Include ("Yuan.class.php");
Include "Yuan.class.php";
Require_once ("Yuan.class.php");
Require_once "Yuan.class.php";
Require "Yuan.class.php";
Require ("Yuan.class.php");
$y 1=new Yuan (10);
$y 2=new Yuan (5);
echo $y 1->mianincluji ()-$y 2->mianji ();

2. Use an object-oriented approach to display data from an info table on a page

Show Data:

Attached: When linking a table to the data to be displayed,

Tables and classes correspond: Table names are class names, and column names are members of a class

Each piece of data in the table corresponds to the object instantiated by the class

Practice:

1. Create a new file, do an info table class, the table name is Info.class.php, inside is pure PHP code

<?php

entity classes for info tables

Class Info

{

Public $code;

Public $name;

Public $sex;

Public $nation;

Public $birtday;

}

2. Refer to this class in the source code:

Include "Info.class.php";

Start making the data in the info table:

$arr =array ();

$info 1=new Info ();

$info 1->code= "P001";

$info 1->name= "Zhang San";

$info 1->sex= "Male";

$info 1->nation= "Han";

$info 1->birthday= "1977-03-13";

Append an element to the array and place the object in an array with Array_push ()

Array_push ($arr, $info 1);

$info 2=new Info ();

$info 2->code= "P002";

$info 2->name= "John Doe";

$info 2->sex= "female";

$info 2->nation= "Hui";

$info 2->birthday= "1988-03-13";

Array_push ($arr, $info 2);

$info 3=new Info ();

$info 3->code= "P003";

$info 3->name= "Wang Wu";

$info 3->sex= "Male";

$info 3->nation= "Miao";

$info 3->birthday= "1983-03-13";

Array_push ($arr, $info 3);

echo "<table border= ' 1 ' width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' >";

echo "<tr><td> code </td><td> name </td><td> Gender </td><td> National </td> <td> birthday </td></tr> ";

foreach ($arr as $value)

{

echo "<tr>

<td>{$value->code}</td>

<td>{$value->name}</td>

<td>{$value->sex}</td>

<td>{$value->nation}</td>

<td>{$value->birthday}</td>

</tr> ";

}

echo "</table>";

3. Make a game hero

<1.> Create a new PHP file for hero, named Hero.class.php

Class Hero
{

Public $name;
Public $blood;
Public $attact;
Public $experience;
Public $level;
Public $money;
Public $skill = Array ();


constructor to initialize a member
function __construct ($n)
{

$this->name = $n;
$this->blood = 100;
$this->attact = 10;
$this->experience=0;
$this->level=1;   
$this->money = 100;  
}

//Punch function
Function Daguai ()
{
//kill monster randomly get a value and take an integer
$SJ = Floor (rand (0,100));
    if ($SJ >30)  //random get value greater than 30 increased XP
{
//get experience
$exp = Floor (rand (0,40));
    Add the Hero experience
$this->experience = $this->experience+ $exp;
      Determine if you want to upgrade
if ($this->experience>=50)
{
$this->level +=1;
$this->experience= 0;
$this->blood + = 20;
$this->attact +=5;
}
Echo $this->name. " Killed a monster, gained {$EXP} point experience <br> ";
}
Else
{
if ($this->level==1)
{
}
Else
{
$this->level-=1 ;  
}
Echo "You were killed by a monster <br>";
}
}


View hero Information
function Show ()
{
echo "Hero Name: {$this->name}<br>";
echo "Heroic blood Volume: {$this->blood}<br>";
echo "Hero attack: {$this->attact}<br>";
echo "Hero experience: {$this->experience}<br>";
echo "Hero level: {$this->level}<br>";
echo "Skill is:";
foreach ($this->skill as $v)
{
echo $v. "!";
}
}


Learning Skills
function Xuexi ()
{
Money
$HF = Floor (rand (0,20));
$n = Floor (rand (0,5)); 5 Kinds of Skills random learning


Skill Base Selection Skills
Switch ($n)
{
Case 1:
Array_push ($this->skill, "charge");
Break
Case 2:
Array_push ($this->skill, "taunt");
Break
Case 3:
Array_push ($this->skill, "lethal strike");
Break
Case 4:
Array_push ($this->skill, "shield Wall");
Break
Case 5:
Array_push ($this->skill, "silent");
Break
}
}
}

<2.> Source code:

Include ("Hero.class.php");

$hero = new Hero ("Wu Song"); Create a Hero

$hero->daguai ();

$hero->xuexi ();
$hero->show ();

PHP and the Like, objects (iv) loading classes and exercises

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.