search
Homephp教程php手册php入门教程之对象详解(1/5)

php入门教程之对象详解(1/5)

Jun 13, 2016 am 10:15 AM
phpGetting Started TutorialcontentpolymorphismobjectinterfaceusekindDetailed explanationmagic

php中对象包括很多内容,如最常用的php类,接口,多态性 魔术方法(:_construct(),_destruct(),_clone)等。

类的声明:

 

 代码如下 复制代码

    权限修饰符 class 类名{   //权限修士符号:public,protected,private 或者省略3者.
      //类体;        //class 是建类关键字
    }             //类名必须跟在class 后面,且跟上{}.{}之间放类的成员.
  ?>
//ps:在class关键字前可以加权限修饰符外,还可以加static,abstract等关键字.一个类,即一对大括号之间的全部内容都要在一段代码段中,不允许将类中的内容分割成对块.
  class ConnDB{
    //....
?>

    //...
  };
?>

 

成员属性:

  在类中直接声明的变量称为成员属性/变量.其类型可以为php中的标量类型和复合类型,使用资源类型和空类型是无效的.

此外,成员属性的声明时,必须要有关键字来修饰:有特定意义的关键字:public,protected,private ;不需要特定意义:var.声明成员属性时,没有必要赋初始值.

 

成员常量:

  以const常量修饰,例如:const PI = 3.1415926;

  常量的输出不需要实例化,直接由类名+常量名调用即可,格式为: 类名::常量名

ps. 特殊的访问方法:--------"$this" 和 "::"

1) $"this" 存在于每个成员方法当中,它是一个特殊的对象以用方法.成员方法属于那个对象,$this应用就代表那个对象,其作用就是专门完成对象内部成员之间的访问.

2) "::"成为作用域操作符,使用这个操作符可以在不创建对象的情况下调用类中的常量,变量和方法. 其语法格式如下:

  关键字::变量名/常量名/方法名

  关键字:parent,可以调用父类成员中的成员变量,成员方法和常量;

      self,可以调用当前类中的静态成员和常量;

      类名,可以调用类中的常量,变量和方法;   

  

成员方法:

  在类中声明的函数成为成员方法,在一个类中可以声明多个函数,即对象可以拥有多个成员方法.成员方法的声明和函数的声明相同,唯一特殊之处就是成员方法可以有关键字对它进行修饰,从而控制其访问权限.

类的实例化

  创建对象:

    $变量名 = new 类名称([参数]); //类的实例化.

  访问类成员:

    $变量名 -> 成员属性 = 值;

1 2 3 4 5

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools