PHP入门教程之面向对象基本概念实例分析

2019-05-03 02:07:17王冬梅

Demo5.php

<?php
  header('Content-Type:text/html; charset=utf-8;');
  class Computer {
    public $_name; //public 表示共有,类外可以访问
    public $_model;
    //创建方法的格式:修饰符 function 方法名(){}
    //如果不加修饰符,默认就是public
    function _run($_who){
      echo $_who.'是运行的方法';
    }
  }
  //创建一个对象,生产出一台电脑 -> 表示指向
  $computer1 = new Computer();
  $computer1 -> _run('一站式建网站');
?>

Demo6.php

<?php
  header('Content-Type:text/html; charset=utf-8;');
  class Computer {
    //创建一个构造方法
    public function Computer(){
      echo '我是构造方法';
    }
  }
  //只要实例化,就可以运行构造方法
  //$computer = new Computer();
  new Computer();
?>

Demo7.php

<?php
  header('Content-Type:text/html; charset=utf-8;');
  class Computer {
    //创建一个构造方法
    public function __construct(){
      echo '我是比较先进的构造方法';
    }
  }
  //只要实例化,就可以运行构造方法
  //$computer = new Computer();
  new Computer();
?>

Demo8.php

<?php
  header('Content-Type:text/html; charset=utf-8;');
  class Computer {
    //创建一个构造方法
    public function __construct(){
      echo '我是比较先进的构造方法';
    }
    //析构方法
    public function __destruct(){
      echo '我是析构方法';
    }
    //普通方法
    public function _run(){
      echo '我是普通方法';
    }
  }
  //只要实例化,就可以运行构造方法
  $computer = new Computer();
  $computer -> _run();
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

相关文章 大家在看