Blogger Information
Blog 26
fans 1
comment 0
visits 22355
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 第课: PHP对象 ,创建类、实例化、命名空间、类成员、类方法 11月26日
孤忆寻昔R
Original
1088 people have browsed it

创建类

关键字class
// 创建对象
class People{

  1. }
  2. class Kors{
  3. }
  4. $o1 = new People();
  5. //var_dump($o1);
  6. $obj2 = new People();
  7. //var_dump($obj2);
  8. echo '</br>';
  9. $obj3 = new Kors();
  10. //var_dump($obj3);
  11. var_dump($obj2 ==$obj3);

实例化

$obj = new p();

  1. class k{
  2. //成员变量
  3. public $name = '笔记本';
  4. public $price = '800';
  5. }
  6. //实例化
  7. $asa = new k();
  8. $asa->name;
  9. echo '设备名称:'.$asa->name;
  10. echo '商品价格'.$asa->price;
  11. $asa->color = "红色";
  12. echo $asa->color;

命名空间

  1. namespace ouyangke;
  2. class Ps{
  3. }
  4. namespace wkos{
  5. class a {
  6. }
  7. }

类成员

  1. class Rowrs{
  2. // 成员属性
  3. public $name='人类';
  4. public $age = '黄皮肤';
  5. public function heshui(){
  6. $this->name;
  7. echo '我要喝水';
  8. }
  9. public function chifan(){
  10. echo '要吃饭';
  11. }
  12. public function newlei(){
  13. $b = new self();
  14. $b->heshui();
  15. var_dump($b);
  16. }
  17. }
  18. $ren = new Rowrs();
  19. $ren->name = '欧阳克';
  20. $ren->chifan();
  21. $ren->newlei();

构造方法

  1. <?php
  2. //构造方法和析构方法
  3. class A{
  4. public $name;
  5. public $age;
  6. public function __construct($name,$age){
  7. $this->name = $name;
  8. $this->age = $age;
  9. echo $this->name.$this->age;
  10. }
  11. public function chifan($chi){
  12. echo '要'.$chi;
  13. }
  14. }
  15. $a = new A('ooyk',25);
  16. //$a = new A('prete',25);
  17. //echo '<hr>';
  18. $a->chifan('土豆丝');

总结

<?php
1.在同类中new出来的对象 不能恒等于===
2.实例化对象 关键字 new
3.命名空间 namespace
4.类成员 可以有三个属性 public protected private
5.实例化对象传参就给 构造方法的传参
6.$this 是实例化对象类 self 对象本类

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看样子应该是没有写完, 希望有空补齐, 过
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post