PHP Basics and the like

Source: Internet
Author: User
Tags php basics

How to access methods in a class:

Class A
{
function foo ()
{

}
}

1.a::foo ();

2. $a = new A (); $a->foo ();

3. $className = ' A '; $instance = new $className (); $instance->foo ();

Note: An available pseudo-variable exists for an Claa object$this,第一种不存在$this变量,因为它不是通过new对象方式访问的,第二种存在$this,第三种存在

Continuation of the class

Using the extends keyword, you can override the method of the parent class with the same naming method, and if you want to access the overridden parent method, use the parent keyword

Class Extendclass extends Simpleclass
{
Redefine the Parent method
function Displayvar ()
{
echo "Extending class\n";
Parent::d Isplayvar ();
}
}

:: Class

Gets the full class name of the class

Namespace NS {
Class ClassName {
}

echo classname::class;//ns\classname

}

class Constants : const constant = ' constant value ';

Auto Load class file: function __autoload ($class _name) {
Require_once $class _name. '. php ';
}

constructor function

The __construct () constructor is automatically called when the new object

Class BaseClass {
function __construct () {
Print "in BaseClass constructor\n";
}
}

Permissions

Class myclass
{
    public  $public  =  ' public ';
    protected  $protected  =  ' protected ';
    private  $private  =  ' private ';

    function printhello ()
    {
         echo  $this->public;
        echo  $this->protected;
        echo  $this->private;
    }
}

$obj  = new myclass ();
echo  $obj->public; //  This line can be executed normally
echo  $obj->protected; //  This row produces a fatal error
echo  $obj->private; //  This line will also produce a fatal error
$obj->printhello (); //  output  public, protected  and  private

============================================================================

Class myclass
{
    //  declares a public constructor
    public  function __construct ()  { 

    //  declares a public method
     public function mypublic ()  { 

    //  Declares a protected method
    protected function myprotected ()  { }

     //  declare a private method
    private function myprivate ()  {  }

    //  This method is public
    function foo ()
     {
         $this->mypublic ();
         $this->myprotected ();
         $this->myprivate ();
    }
}

Trait
Trait Ezcreflectionreturninfo {
function Getreturntype () {/*1*/}
function Getreturndescription () {/*2*/}
}

Class Ezcreflectionmethod extends Reflectionmethod {
Use Ezcreflectionreturninfo;
/* ... */
}
$myclass = new MyClass;
$myclass->mypublic (); This line can be executed normally.
$myclass->myprotected (); This guild produces a fatal error.
$myclass->myprivate (); This guild produces a fatal error.
$myclass->foo (); Public, protected, private can be executed

Abstract class

Abstract class AbstractClass
{
Forcing subclasses to define these methods
Abstract protected function getValue ();
Abstract protected function Prefixvalue ($prefix);

Common methods (non-abstract methods)
Public Function PrintOut () {
Print $this->getvalue (). "\ n";
}
}

Interface

Interface A
{
Public function foo ();
}

Interface B extends A
{
Public function Baz (Baz $baz);
}

Correct wording
Class C Implements B
{
Public Function foo ()
{
}

Public function Baz (Baz $baz)
{
}
}

Trait

Trait Ezcreflectionreturninfo {
function Getreturntype () {/*1*/}
function Getreturndescription () {/*2*/}
}

Class Ezcreflectionmethod extends Reflectionmethod {
Use Ezcreflectionreturninfo;
/* ... */
}

PHP Basics and the like

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.