PHP Object-oriented Essentials summary, PHP object-oriented essentials
This article summarizes the Essentials of PHP object-oriented programming. Share to everyone for your reference. The specific analysis is as follows:
1 using extends to implement inheritance and the meaning of overloading, magic methods
Class B extends A
At the time of the Declaration, B could have no way in a.
When called:
$b =new B ();
$b method of->a ();
$b the attribute in->a = 1;
$b method of->b ();
$b method of->b ();
If $a=new a ();
OK
$a method of->a ();
$a the attribute in->a = 1;
No
$a method of->b ();
$a method of->b ();
Overloading: B inherits the method property of a, B, and a with the same name.
"Overloading" in PHP is different from most other object-oriented languages. Traditional "overloads" are used to provide multiple class methods with the same name, but each method has a different parameter type and number.
Magic Method: PHP treats all class methods starting with __ (two underscore) as a magic method. So when you define your own class method, do not prefix __.
2 inheritance with private and protected access modifier visibility
Property methods private cannot be inherited
The protected property method is not visible outside the class and can be inherited
The class member defined by the public property method can be accessed from anywhere
3 PHP Double colon:: Application
The PHP class code often sees the "::" operator, which is scoped to the scope operator, and is represented by a double-colon "::", which is used to pin the levels of different scopes in the class. The left side is the member of the access scope to the right of the scope.
The scopes defined in PHP are self and parent two (a static scope is provided in PHP6).
The scope resolution operator (also known as Paamayim Nekudotayim) or, more simply, a pair of colons, can be used to access static members, methods, and constants, and also for subclasses to override members and methods in the parent class.
Copy the Code code as follows: Class MyClass {
Const Const_value = ' A constant VALUE ';
}
Echo Myclass::const_value;
Class Otherclass extends MyClass
{
public static $my _static = ' static var ';
public static function Doublecolon () {
Echo Parent::const_value. "\ n";
echo Self:: $my _static. "\ n";
}
}
Otherclass::d Oublecolon ();
Child class overrides parent class
Class MyClass
{
protected function MyFunc () {
echo "Myclass::myfunc () \ n";
}
}
Class Otherclass extends MyClass
{
Overriding methods in a parent class
Public Function MyFunc ()
{
But you can still call methods that have been overridden
Parent::myfunc ();
echo "Otherclass::myfunc () \ n";
}
}
$class = new Otherclass ();
$class->myfunc ();
4 This and self and the role of the parent in PHP
This: is a pointer to the current object instance and does not point to any other object or class.
Self: Represents the scope of the current class, unlike this, which does not represent a particular instance of a class, does not work in code other than the class, and it does not recognize its position in the hierarchy of inheritance. That is, when you use self in an extension class, it calls a method other than the parent class, but instead extends the overloaded method of the class. Self is a pointer to the class itself, that is, to any object that has already been instantiated, and it is typically used to point to a static variable in the class.
Copy the Code code as follows: private static $firstCount = 0;
Private $lastCount;
constructor function
function __construct ()
{
$this->lastcount = ++self: $firstCount; Use self to invoke a static variable, using the self call must use::(domain operator symbol)
}
Parent: Represents the scope of the current class parent class, and the rest is the same as the self attribute. Parent is a pointer to the parent class, which we typically use to invoke the parent class's constructor.
Copy the Code Code as follows://constructor for inheriting class
function __construct ($personSex, $personAge)
{
Parent::__construct ("test"); The constructor for the parent class was called with parent
$this->personsex = $personSex;
$this->personage = $personAge;
}
5 Constructors and destructors
Classes with constructors Call this method each time an object is created, making it ideal for doing some initialization work before using the object.
function __construct () {}
If a constructor is defined in a subclass, the constructor for its parent class is not implicitly called. To execute the constructor of the parent class, you need to call Parent::__construct () in the constructor of the child class.
PHP 5 introduces the concept of destructors, similar to other object-oriented languages such as C + +. Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.
function __destruct () {}
6 Final keyword
PHP 5 has a new final keyword. If a method in the parent class is declared final, the subclass cannot overwrite the method, and if a class is declared final, it cannot be inherited.
7 Inheritance and constructors
Parent class |
Sub-class |
Results |
Has a constructor function |
No constructors |
Parent Construct |
Has a constructor function |
Has a constructor function |
Sub-structure |
8 Interface
You can define an interface by interface, just as you would define a standard class.
Attention:
1) But all the methods defined therein are empty;
2) All methods defined in the interface must be public, which is the feature of the interface;
3) When implementing multiple interfaces, the methods in the interface cannot have duplicate names;
4) interfaces can also be inherited by using the extends operator;
5) Constants can also be defined in the interface. Interface constants and class constants are used exactly the same. They are all fixed values and cannot be modified by a quilt class or subinterface.
Copy the code as follows://Declare a ' iTemplate ' interface
Interface ITemplate
{
Public Function SetVariable ($name, $var);
Public Function gethtml ($template);
}
Implementing interfaces
The following is the correct wording
Class Template implements ITemplate
{
Private $vars = Array ();
Public Function SetVariable ($name, $var)
{
$this->vars[$name] = $var;
}
Public Function gethtml ($template)
{
foreach ($this->vars as $name = + $value) {
$template = Str_replace (' {'. $name. '} ', $value, $template);
}
return $template;
}
}
9 Properties
A variable member of a class is called an attribute, and a property declaration is made up of the keyword public or protected or private, followed by a variable. A variable in a property can be initialized, but the initialized value must be a constant, where a constant is a constant that the PHP script will be a constant at compile time, rather than the one that was run at runtime after the compilation phase.
In PHP5, two functions "__get ()" and "__set ()" are predefined to obtain
Take and assign its properties, and examine the property's "__isset ()" and the Method "__unset ()" To delete the property.
The simple one is to take the value, and the other is to assign a value. , "__set ()" and "__get ()" Two methods, these two methods are not the default, but we add to the class by hand, like the construction Method (__construct ()), the class is added to exist, you can add these two methods as follows, Of course, it can also be added as a personal style: the//__get () method is used to get the private property
Copy CodeThe code is as follows: <?php
Class person{
The following is the person's member property
Private $name; Man's name
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the private property
Private Function __get ($property _name) {
if (Isset ($this-$property _name)) {
Return ($this $property _name);} else {
return (NULL);
}
}
}
The __set () method is used to set the private property
Private Function __set ($property _name, $value) {
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM) {
The echo "Isset () function automatically calls when a private member is measured
";
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM) {
echo "When you use the unset () function outside of a class to delete a private member, the automatically called
";
Unset ($this, $nm);
}
}
$p 1=new person ();
$p 1->name= "This was a person name";
When using the Isset () function to measure a private member, the __isset () method is automatically called to help us complete and return the result to True
Echo Var_dump (Isset ($p 1->name)). "
";
echo $p 1->name. "
";
When you use the unset () function to delete a private member, the __unset () method is called automatically to help us complete, removing the name private property
unset ($p 1->name);
has been removed, there is no output for this line
Echo $p 1->name;
?>
Copy CodeThe code is as follows: <?php
Class person{
The following is the person's member property
Private $name;
Man's name
Private $sex;
Man's Sex
Private $age;
The age of the person
The __get () method is used to get the private property
Private Function __get ($property _name) {
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}else{
return (NULL);
}
}
}
The __set () method is used to set the private property
Private Function __set ($property _name, $value) {
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM) {
The echo "Isset () function automatically calls when a private member is measured
";
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM) {
echo "When you use the unset () function outside of a class to delete a private member, the automatically called
";
Unset ($this, $nm);
}
}
$p 1=new person ();
$p 1->name= "This was a person name";
When using the Isset () function to measure a private member, the __isset () method is automatically called to help us complete and return the result to True
Echo Var_dump (Isset ($p 1->name)). "
";
echo $p 1->name. "
";
When you use the unset () function to delete a private member, the __unset () method is called automatically to help us complete, removing the name private property
unset ($p 1->name);
has been removed, there is no output for this line
Echo $p 1->name;
?>
10 cloning
Object replication can be done by using the Clone keyword (if the __clone () method exists in the object, it is called first). The __clone () method in the object cannot be called directly.
When the object is copied, PHP5 performs a "shallow copy" of all properties of the object (shallow copy). The references in all the attributes remain unchanged, pointing to the original variable. If the __clone () method is defined, the __clone () method in the newly created object (copy generated object) is called and can be used to modify the value of the property, if necessary.
It is hoped that this article is helpful to everyone's PHP object-oriented programming.
How do you understand PHP object-oriented?
OOP thought MVC structure you understand what the M V C stands for, the simplest understanding is that the view page needs to show what content to controll processing, controll need data MODEL for database processing supply and then all feedback to Controll, Transfer the contents to the View page display after finishing processing
The simple point is that view is used to show that controll is used to control the processing model for database analysis processing
The idea of OOP is simply to think of all the same type as a container, then focus on the process and feed the data back to the page through the interface.
PHP Object-oriented
PHP operates the basic class of MySQL database, object-oriented annotated. Some additional features are added: Inserting data function insertdata into the specified table ($dbname, $data)
Servername,username,password,dbname these several parameters use
Define ("UserName", "?? ”); Database connection user Name
Define ("PassWord", "?? ”); Database connection Password
Define ("ServerName", "?? ”); Name of the database server
Define ("DBName", "?? ”); Database name
/**
* Function: Insert data into the specified table
* Parameters: $dbname table name, $data array (format: $data [' field name '] = value)
* Return: Insert record ID
*/
Public Function InsertData ($dbname, $data)
{
$field = Implode (', ', Array_keys ($data)); Define the Fields section of the SQL statement
$i = 0;
foreach ($data as $key + $val)//Combined value portion of the SQL statement
{
$value. = "'". $val. "'";
if ($i < count ($data)-1)//Determines whether the last value to the array
$value. = ",";
$i + +;
}
$sql = "INSERT into". $dbname. " (" . $field. ") VALUES (". $value. ")";
return $this->insert ($sql);
}
See the rest of the section for reference, and also note that if you are using a code that is not UTF8, you need to modify the following line mysql_query ("Set names UTF8"); Also limit the encoding type!
mysql_query ("Set names GBK"); GBK encoding Type!
Or
mysql_query ("Set names gb2312"); Also limit the encoding type!
For supplementary questions: it can be combined together to do SQL statements, but the proposal is not too familiar with PHP and SQL is best to write separately, there is no need for a small function to spend a lot of time. If more complicated inserts are excluded!
Reference: ruyihe.com/blog/2010/10/php the base class for manipulating MySQL databases, object-oriented annotated/
http://www.bkjia.com/PHPjc/907841.html www.bkjia.com true http://www.bkjia.com/PHPjc/907841.html techarticle PHP Object-oriented Essentials summary, PHP Object-oriented Essentials This article summarizes the Essentials of PHP object-oriented programming. Share to everyone for your reference. The specific analysis is as follows: 1 using Exten ...