PHP入門之數組+函數+類與面向對象

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聯繫:[email protected]  // 備註 CSDN
github:https://github.com/yline


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章