zoukankan      html  css  js  c++  java
  • OOP in php

    创建一个类

     1 class Person{
     2     //类的属性 相当于变量
     3     public $firstname;
     4     public $lastname;
     5     public $age;
     6 
     7    //construct方法 类当中的方法相当于php里的函数
     8     public function __construct(){
     9         //对于不同的类 我们可以有不同的属性
    10         $this->firstname = $firstname;
    11         $this->lastname = $lastname;
    12         $this->age = $age;
    13 
    14     
    15     }
    16 
    17     //类的方法
    18     public function greet(){
    19         return "Hello, i am ".$this->firstname." ".$this->lastname.".";
    20     }
    21 
    22     
    23 
    24 
    25 }
    26 
    27 //创建一个类的对象
    28 $student1 = new Person("linux","root",20);
    29 echo $student1->greet();//输出Hello, i am linux root.
    30 $student2 = new Person("Lin","lin",20);
    31 echo $student2->greet();//输出Hello, i am Lin lin.
  • 相关阅读:
    wenbao与分治
    wenbao与贪心
    wenbao与高精度
    wenbao与FFT
    wenbao与卡特兰数
    wenbao与类
    wenbao与取整函数
    wenbao与字符串转化
    wenbao与scanf
    wenbao与数字与字符串
  • 原文地址:https://www.cnblogs.com/linuxroot/p/3107970.html
Copyright © 2011-2022 走看看