Several important object-oriented methods

Declare a class, defining a default public

Syntax class name { 

}

 

Declare variables in the class

Syntax class name {
 var $ A; 
}

In a statement within the class method

语法 class name{
var $a;
function fun(){
}
}

If you want to use variables and methods in a class, you must first instantiate this class is instantiated with the new keyword,

After an object is instantiated, it may be used at this time the internal variables and methods

aname=new name();

Want to use variables and methods, with the symbol ->

aname-> a. 
aname -> fun.

Rewrite (inherited)

Methods btest inherited methods atest, the btest get variables and methods of atest, of course, also can redefine variables and methods, is called Rewrite

<?php
class atest{

    function a()
    {
        echo "haha";
    }
}
class btest extends atest
{

}

$test=new btest();
$test->a(); //haha

In one class, if the definition of static variables and methods, there is no need to re-instantiate the call, it may be used directly in this class self :: call using external :: output directly

Call in the current class , ordinary methods and variables using $ this-> , static variables and methods of using self ::

 

class atest
{
public static $a="haha"; public static function b()
{ echo
"你在".self::$a; } } echo atest::$a."<br>"; //haha echo atest::b(); //你在haha

If you want to call static methods and variables of the parent class in a subclass use parent :: to call

class atest{

    public static function a()
    {
        echo "123";
    }
}
class btest extends atest
{
     function test(){
         echo parent::a();
     }
}
$fun=new btest();
echo $fun->test();  //123

Constructor constraint need not be declared ,   in the instance when the class , automatic operation using __construct

Destructor same constraints need not be declared , is invoked after the completion of the object , it is automatically performed using __destruct

The interface declaration keyword interface   using the keyword implemtns   , methods need are public public

Interfaces need to implement functions specific methods

Class using the interface , the interface need to implement all of the methods

 

Guess you like

Origin www.cnblogs.com/dumenglong/p/11184458.html