This article illustrates the basic object-oriented concept of PHP. Share to everyone for your reference, specific as follows:
demo1.php
<?php
//How to create a class format: Modifier class class Name {}
//We go to create a computer class that can create objects (produce computer) class
Computer {//class name first letter uppercase
}
//Create a computer out, that is, the object's declaration
//FORMAT: variable = new class name ();
The new Compuer () represents the process of instantiating (meaning creating an object)
//$compuer 1 = new Compuer () This process is to give the address of the instantiated object to $compuer 1
//$compuer 1 We can call it the application of the object
$computer 1 = new Computer (); This is the first computer we created
$computer 2 = $computer 1; This is the second computer we created
Var_dump ($computer 1);
Echo ' <br/> ';
Var_dump ($computer 1);
? >
demo2.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {//// field member declaration format: modifier variable name [=xxx];
Public $_name = ' Association '; Public means shared, outside class can access
//public $_model = ' i7 ';
/////
Create an object to produce a computer-> indicating the point
//$computer 1 = new Computer ();
echo $computer 1-> _name;
$computer 1-> _name = ' dell ';
echo $computer 1-> _name;
Class Computer {
//field member declaration format: modifier variable name [=xxx];
Public $_name; Public indicates a common, outside class can access the public
$_model;
Create an object that produces a computer-> that points to
$computer 1 = new Computer ();
Assign values to member fields
$computer 1-> _name = ' Lenovo ';
Value
echo $computer 1-> _name;
? >
demo3.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {public
$_name;//public represents a common, outside class can access the public
$_model;
Format of creation Method: Modifier function Method name () {}
///without modifiers, the default is public
function _run () {
echo ' I am the method of running ';
}
}
Create an object that produces a computer-> that points to
$computer 1 = new Computer ();
$computer 1-> _run ();
? >
demo4.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {//// field member declaration format: modifier variable name [=xxx];
Public $_name = ' Association '; Public means shared, outside class can access
//public $_model = ' i7 ';
/////
Create an object to produce a computer-> indicating the point
//$computer 1 = new Computer ();
echo $computer 1-> _name;
$computer 1-> _name = ' dell ';
echo $computer 1-> _name;
Class Computer {
//field member declaration format: modifier variable name [=xxx];
Public $_name; Public indicates a common, outside class can access the public
$_model;
Create an object that produces a computer-> that points to
$computer 1 = new Computer ();
Assign values to member fields
$computer 1-> _name = ' Lenovo ';
The value of
//echo $computer 1-> _name;
$computer 2 = $computer 1;
echo $computer 2-> _name;
? >
demo5.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {public
$_name;//public represents a common, outside class can access the public
$_model;
Format of creation Method: Modifier function Method name () {}
///without modifiers, the default is public
function _run ($_who) {
echo $_who. ' Is the method to run ';
}
//Create an object that produces a computer-> that points to
$computer 1 = new Computer ();
$computer 1-> _run (' One-stop building site ');
? >
demo6.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {
//Create a constructor public
function Computer () {
echo ' I am constructor method ';
}
}
As long as the instantiation, you can run the construction method
//$computer = new computer ();
New Computer ();
? >
demo7.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {
//Create a constructor public
function __construct () {
echo ' I am the more advanced construction method ';
}
}
As long as the instantiation, you can run the construction method
//$computer = new computer ();
New Computer ();
? >
demo8.php
<?php
header (' content-type:text/html; charset=utf-8; ');
Class Computer {
//Create a constructor public
function __construct () {
echo ' I am the more advanced construction method ';
}
destructor Public
function __destruct () {
echo ' I am a destructor ';
}
Common method Public
function _run () {
echo ' I am normal method ';
}
}
As long as the instantiation, you can run the construction method
$computer = new computer ();
$computer-> _run ();
? >
For more information about PHP interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP Array" operation Techniques Daquan, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.