PHP物件導向基礎

來源:互聯網
上載者:User

標籤:des   c   class   a   color   int   

<?php
//PHP oop學習
class Person{

public $name;
public $age;
public $height;

function getInstance($name,$age,$height){
$obj_person = new Person();
$obj_person->name = $name;
$obj_person->age = $age;
$obj_person->height = $height;
return $obj_person;
}
}

$obj_person = new Person();

$class_data = $obj_person->getInstance("陳彬", 100, 171);

var_dump($class_data);
?>

PHP 類名規範 一般為類名.class.php

Person.class.php

記憶體分析:
堆記憶體:放對象的東西

棧記憶體:放基礎資料型別 (Elementary Data Type)(若是基礎資料型別 (Elementary Data Type)屬於某個對象,那麼應該也放在堆記憶體)

函數開啟,開闢新棧記憶體 函數調用完畢 立即回收記憶體

靜態記憶體:靜態


常量記憶體:常量

代碼記憶體:代碼指令


---------------------------
===========================
---------------------------


//關於記憶體的劃分,堆記憶體,棧記憶體,常量記憶體,待用資料記憶體,代碼記憶體
// question :調用函數傳入對象 和基礎資料型別 (Elementary Data Type) 傳入的到底是記憶體位址,還是值

$obj = new Person();
function for_obj($obj){
$obj->name = "chenbin";
}
for_obj($obj);
var_dump($obj);

//當傳入對象的時候,傳入的是對象的記憶體位址,所以我們通過地址找到空間,對值做修改。
//所以傳對象 實際傳的是對象的記憶體位址
$bin_a = 1;


function for_comm($bin_a){
$bin_a = 3;
print_r("===");
}
for_comm($bin_a);
var_dump($bin_a);
//當傳入的基礎資料類型 實際上傳入的是值,因為我們無法找到空間,對值進行修改。

$arr_a = array(1,2,3,4);
function for_arr(&$arr_a){
$arr_a[3] = 100;
}
function for_arr_1($arr_a){
$arr_a[3] = 100;
}
for_arr_1($arr_a);
var_dump($arr_a);

for_arr($arr_a);
var_dump($arr_a);

//數組傳參數,預設情況下是傳的值而不是地址


//注意
*/
//在類中,函數中若要引用函數外部的變數,需要用$this->xx 而不能直接用$變數名稱
class Pig{
public $weight;

function weightAdd($num){
//$weight = $weight+$num;
$this->weight = $this->weight+$num;
}
}

$pig = new Pig();
$pig->weightAdd(4);
var_dump($pig->weight);


?>

====================================
------------------------------------
====================================
PHP構造方法

 

class Mobile{

public function __construct(){
echo $this->color;
}
//這說明建構函式是對象建立後才調用的
public $color = "black";


//PHP5 以下是這麼的寫法
public function Mobile(){
echo $this->color;
echo "-----";
}
}

$mobile = new Mobile();

PHP解構函式
definition:在某個對象的所有引用都被刪除或者對象被顯式銷毀的時候執行

主要用來釋放資源 比如關閉資料庫連結


解構函式調用的順序先建立的對象 後被銷毀

棧 是先進後出

解構函式被調用的時候:

A 當程式退出時

B 當對象成為垃圾對象的時候,該對象立刻調用該對象的解構函式

//php 解構函式

class ball{
public $name;
public function __construct($name){
$this->name = $name;
}
public function __destruct(){
echo $this->name."刪除中\n";
}
}

$c1 = new ball("111");
$c1 = null;//A 當程式退出時 B 當對象成為垃圾對象的時候,該對象立刻調用解構函式
$c2 = new ball("222");
$c3 = new ball("333");

//解構函式執行的順序 倒過來了和new處對象的引用存在棧記憶體中有關係,因為棧是先入後出

//PHP 靜態變數

*/
//靜態變數在類中用self::變數名 用$this->變數名不行,因為靜態變數是屬於類的而不是對象,所以用this當前對象去調用這是不合適的
/* class Cat{

publicstatic $a = 0;

public function __construct($num){
self::$a += $num;
}
}
$a_1 = new Cat(12);
$a_2 = new Cat(12);
var_dump($a_2::$a); */

PHP靜態方法
//靜態方法

class Static_study{
public $a = 4;
public static $b = 5;
private static $c = 6;
protected static $d = 7;

public static function test(){
//echo $this->a; //靜態方法中不能引用非靜態成員變數
echo self::$b."\r";
}

public function __construct(){
Static_study::test();
self::test();
//self:: 能獲得類內部任何修飾符的變數
//Static_study::能獲得類內部任何修飾符的變數
}
}

$study_study = new Static_study();
$study_study->test();
$study_study::test();
Static_study::test();
//$study_study->無法擷取private 和 protected 的靜態變數和資料
//$study_study::無法擷取private 和 protected 的靜態變數和資料
//Static_study:: 無法擷取private 和 protected 的靜態變數和資料
//類外部調用類內部靜態變數和靜態函數 都可用上面三種方式


//var_dump($this->$a); 這樣式行不通的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.