PHP入门之数组+函数+类与面向对象

来源:互联网 发布:木门生产软件 编辑:程序博客网 时间:2024/05/17 06:49

1,数组

1,初始化

  1. $arr = array(); // 新建空数组
  2. $demo = array(
  3. '1' => 'B',
  4. '2' => 'Fuck'
  5. );// key->value形式数组
  6. $sample = array(
  7. 'C',
  8. 'D'
  9. );// 0->value形式数组
  10. $fruit = array(
  11. "苹果",
  12. "香蕉",
  13. "菠萝"
  14. );// 0->value形式数组
  15. print_r($arr);
  16. print_r($fruit);
  17. print_r($demo);
  18. print_r($sample);

2,被赋值
  1. $fruit[0] = "A";
  2. $fruit['1'] = "B";
  3. print_r($fruit);

3,赋值
  1. $temp = $fruit['0'];
  2. print_r($temp);

2,函数

1,定义函数

  1. testFunction();
  2. function testFunction()
  3. {
  4. echo 'this is a test function';
  5. }

2,函数赋值给变量(将函数作为一个变量看待)
  1. $name = 'testFunction';
  2. $name();

3,系统函数,介绍几个
  1. $str = '苹果很好吃。';
  2. $str = str_replace('苹果', '香蕉', $str); // 替换字符串
  3. echo $str;
  4. echo function_exists('testFunction')."function\n"; // 函数是否存在
  5. echo class_exists('index')."class\n"; // 类是否存在
  6. echo file_exists('index')."file\n"; // 文件是否存在

3,类与面向对象

1,类介绍(注释中已经 写全)

  1. class Car
  2. {
  3. /* ---------------------------访问修饰符--------------------------- */
  4. private $tempA = 'A'; // 本类 ok
  5. protected $tempB = 'B'; // 本类 ok , 子类 ok
  6. public $tempC = 'C'; // 本类 ok , 子类 ok , 其它类 ok
  7. var $name = '汽车'; // 本类 ok , 子类 ok , 其它类 ok , 等同于 public
  8. /* ---------------------------特殊函数--------------------------- */
  9. /**
  10. * new Car() 时调用
  11. * 构造函数
  12. */
  13. function __construct()
  14. {
  15. echo "Car __construct" . "\n";
  16. }
  17. /**
  18. * unset($car)时,调用
  19. * 析构函数
  20. */
  21. function __destruct()
  22. {
  23. echo "Car __destruct" . "\n";
  24. }
  25. /**
  26. * 当调用不存在的方法的时候,将会转为参数调用__call方法
  27. *
  28. * @param unknown $name
  29. * @param unknown $arg
  30. */
  31. public function __call($name, $arg)
  32. {
  33. if ($name == 'speedDown')
  34. {
  35. $this->speed -= 10;
  36. }
  37. }
  38. /**
  39. * 复制一个 car对象
  40. * clone一个类
  41. */
  42. public function __clone()
  43. {
  44. $obj = new Car();
  45. $obj->name = $this->name;
  46. }
  47. /* ---------------------------静态变量--------------------------- */
  48. private static $fast_speed = 10;
  49. /**
  50. * 静态方法
  51. *
  52. * @return number
  53. */
  54. public function getFastSpeed()
  55. {
  56. return self::$fast_speed;
  57. }
  58. /**
  59. * 静态方法
  60. */
  61. public static function fastSpeedUp()
  62. {
  63. return self::$fast_speed += 10;
  64. }
  65. /* ---------------------------常用变量--------------------------- */
  66. public $speed = 0;
  67. function getName()
  68. {
  69. return $this->name;
  70. }
  71. /**
  72. * $speed 不会改变 Car类中 speed 的值
  73. */
  74. function speedUp()
  75. {
  76. $speed += 10;
  77. }
  78. /**
  79. * $this->speed 会改变 Car类中 speed 的值
  80. */
  81. function speedUp2()
  82. {
  83. $this->speed += 10;
  84. }
  85. }
  86. /* ---------------------------继承关系--------------------------- */
  87. class BigCar extends Car
  88. {
  89. function __construct()
  90. {
  91. echo "BigCar __construct" . "\n";
  92. parent::__construct();
  93. }
  94. }

有技术上的问题,或者想法,欢迎来交流
QQ联系:957339173@qq.com  // 备注 CSDN
github:https://github.com/yline