Heim >Backend-Entwicklung >PHP-Tutorial >第三节--定义一个类 -- Classes and Objects in PHP5 [3]_PHP教程

第三节--定义一个类 -- Classes and Objects in PHP5 [3]_PHP教程

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-20 11:03:441026Durchsuche

 
第三节--定义一个类
当你声明一个类,你需要列出对象应有的所有变量和所有函数—被称为属性和方法. 3.1.1中显示了一个类的构成. 注意在大括号({})内你只能声明变量或者函数. 3.1.2中显示了如何在一个类中定义三个属性和两个方法.
3.1.1
class Name extends Another Class
{
     Access Variable Declaration
     Access Function Declaration
}
 
3.1.2
     //定义一个跟踪用户的类
     class User
     {
         //属性
         public $name;
         private $password, $lastLogin;
         //方法
         public function __construct($name, $password)
         {
        $this->name = $name;
        $this->password = $password;
        $this->lastLogin = time();
        $this->accesses++;
         }
         // 获取最后访问的时间
         function getLastLogin()
         {
        return(date("M d Y", $this->lastLogin));
         }
     }
     //创建一个对象的实例
     $user = new User("Leon", "sdf123");
     //获取最后访问的时间
     print($user->getLastLogin() ."
");

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445267.htmlTechArticle第三节--定义一个类 当你声明一个类,你需要列出对象应有的所有变量和所有函数被称为属性和方法. 3.1.1中显示了一个类的构成. 注意在大括...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn