PHP class class and Object learning notes

Source: Internet
Author: User
Tags abstract constant deprecated php class

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

The code is as follows Copy Code

<?php
/*******************************************

Class object Instance description (normal Class)

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

private $PrivateVar;//external variable cannot be invoked

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 static-too methods and variables

Const constvar= ';//When you have an attribute 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 an initial value when defined
2. Constants cannot be modified.
3. Constant names cannot have $, typically uppercase, default is public
4. Constants Use the self:: the name of a constant in the class:: Name of the constant
*/
Public function __construct (/* $name//) {//This can take parameters, with parameters must be created with parameters, or there will be errors
$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<br/>";
}

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

When this keyword is used to modify a class, then the class will not be inherited by other classes (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
Declaration as abstract

4. Abstract classes cannot have function bodies

5. If a class continues an abstract class, then he must implement the
All abstract methods of the abstract class. (unless it itself is also declared 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 requires that the class must implement all the methods of this interface
2. Interface methods can not have the method body
3. Cannot instantiate an interface
4. An interface can have properties, but it must be constant, and it is public

When to use the interface

1. Fixed specification
2. Set specifications for other programmers to implement, such as:

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

}

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

Inheriting interfaces
Ways to implement the parent interface when inheriting interfaces

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

Const NAMEVAR=20;

}

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

Implements interfaces that enable multiple interfaces to be implemented simultaneously
When a class implements some interfaces, it must implement all of the interface's methods.

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

Public $Name 1=0;

Public Function Name () {

Echo Face2::namevar;

}

}

?>

Summary

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

The code is as follows Copy Code

Slef:: $country
Slef:: Mycountry ()

To access a static member property or method of a parent class in a subclass, use Parent:: (note that it is 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 names::, for example:

The code is as follows Copy Code

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

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


• Class/Object Functions

__autoload-tries to load an undefined class
call_user_method_array-invokes a user method, passing the parameter array (deprecated)
call_user_method- Invoke a user method (deprecated) on a specific object
class_alias-Create an alias for a class
class_exists-Check whether the class is defined
get_called_class-late static binding ("Late static Bin Ding ") class name
get_class_methods-Returns an array of method names of the class
get_class_vars-returns an array of the class's default properties
get_class-Returns the object's class name
get _declared_classes-returns an array of the names of the defined classes
get_declared_interfaces-Returns an array containing all declared interfaces
get_declared_traits- Returns an array of all defined traits
get_object_vars-returns an associative array of object properties
get_parent_class-returns the parent class name of the object or class
interface_exists-Check interface Whether the
is_a-returns True if the object belongs to the class or the class is the parent of this object
is_subclass_of-If this object is a subclass of the class, returns True
method_exists-Check if the method of the class exists br> property_exists-checks whether an object or class has this property
trait_exists-Check if the specified trait exists

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.