PHP class and Object learning Note _php Tutorial

Source: Internet
Author: User
Tags deprecated
In the study written in a PHP class and object learning notes, the following share with friends, I hope this tutorial for you to learn classes and objects helpful.

Personal notes made ..... For reference, if wrong please point out, thank you

The code is as follows Copy Code

/*******************************************

Class object Instance description (normal Class)

*******************************************/
Class classdemo{
Public $PublicVar;

private $PrivateVar;//external variable cannot be called

protected $ProtectedVar;//protected variable external and subclass inaccessible

public static $StaticVar =0;//static static variables static methods cannot access non-static methods and variables, non-static methods and variables can access the static method and the variable

Const constvar= ';//When you have a property that you do not want to be modified, consider using const as a constant, using the class name:: Constant name; Interface Name:: constant
/*
1. Constants need to be assigned the initial value when defined
2. Constants cannot be modified.
3. Constant name cannot have $, usually uppercase, default is public
4. Constants are used inside the class self:: The name of the constant class name:: The name of the constant
*/
Public function __construct (/* $name */) {//This can take parameters, must be created with parameters after it is required, or it will be an error
$this->publicvar= $name;
Self:: $StaticVar ++;//static Variable internal access method external access Method object name:: Variable name (classdemo:: $StaticVar)
$this->publicvar= ' $PublicVar ';
$this->privatevar= ' $PrivateVar ';
$this->protectedvar= ' $ProtectedVar ';
Self:: $StaticVar + +;
echo "Classdemo __construct
";
}

Public Function __destruct () {//destructor
}
Final public Function fun () {
/*
Final keyword (appears in PHP5)
You want the method not to be overwritten by other class inheritance time, you can use the final

When the keyword is used to modify a class, the class will not be inherited by other classes (which can be instantiated)

Note: This keyword cannot be used to modify variables

*/
}
}//end Class
/*******************************************

Class object Instance description (abstract class)

*******************************************/
Abstract class abstractdemo{
/***************************************

1. Abstract classes cannot be instantiated

2. Abstract classes do not necessarily include an abstract method.
In other words, abstract classes can have no abstract method

3. Once the abstract method is included, the class must be
Declare as abstract

4. Abstract class cannot have function body

5. If a class continues with an abstract class, he must implement
All the abstract methods of the abstract class. (unless it declares itself as an abstract class)

***************************************/
}

/*******************************************

Class object Instance description (inheriting Class)

*******************************************/
Class Demotwo extends classdemo{

}
/*******************************************

Class Object Instance Description (interface)

*******************************************/
Interface face{
/*********************************
1. When a class implements an interface, it is required that the class must implement all the methods of this interface
2. The method of the interface cannot have the method body
3. Unable to instantiate an interface
4. The interface can have attributes, but must be constant and public

Where to use the interface

1. Fixed specifications
2. Define the specifications for other programmers to implement, such as:

**********************************/
Public function Name ();

}

Interface Face2 extends face{
/*******************************

Inheriting interfaces
No way to implement the parent interface when inheriting the interface

********************************/

Const NAMEVAR=20;

}

Class Demo implements face2{
/**********************************

Implement interface, can implement multiple interfaces simultaneously
When a class implements certain interfaces, it is necessary to implement the methods of all interfaces.

**********************************/

Public $Name 1=0;

Public Function Name () {

Echo Face2::namevar;

}

}

?>

Summary

To access static member properties or methods inside a class, use self:: (note not $slef), such as:

The code is as follows Copy Code

Slef:: $country
Slef:: Mycountry ()

To access the parent class static member property or method in the subclass, use Parent:: (note not $parent), such as:

The code is as follows Copy Code

Parent:: $country
Parent:: Mycountry ()

External access static member properties and methods are class name/subclass name::, such as:

The code is as follows Copy Code

Person:: $country
Person::mycountry ()
Student:: $country

However, static methods can also be accessed by means of ordinary objects.


• Class/Object functions

__autoload-attempting to load an undefined class
call_user_method_array-invokes a user method, passing a parameter array (deprecated)
call_user_method-invoking a user method on a specific object (deprecated)
class_alias-Creating an alias for a class
class_exists-Checking whether a class is defined
get_called_class-late static binding ("late static binding") class name
get_class_methods-Returns an array of the method names of the class
get_class_vars-Returns an array that consists of the default properties of the class
get_class-returns the class name of the object
get_declared_classes-Returns an array consisting of the names of the defined classes
get_declared_interfaces-returns an array containing all the declared interfaces
get_declared_traits-Returns an array of all defined traits
get_object_vars-returns an associative array consisting of object properties
get_parent_class-returns the parent class name of an object or class
interface_exists-Check if the interface has been defined
is_a-returns TRUE if the object belongs to the class or if the class is the parent class of this object
is_subclass_of-returns TRUE If this object is a subclass of the class
method_exists-checking the existence of a class's methods
property_exists-checking whether an object or class has this property
trait_exists-checks if the specified trait exists

http://www.bkjia.com/PHPjc/632631.html www.bkjia.com true http://www.bkjia.com/PHPjc/632631.html techarticle in the study written in a PHP class and object learning notes, the following share with friends, I hope this tutorial for you to learn classes and objects helpful. Personal notes made ..... To borrow ...

  • 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.