close

下面內容非常零碎,請自行選擇觀看

 

Class A {

var $string;

public $string;

//  var equalls to public.

}

 

在PHP5.0~5.1.3之間使用var會產生E_STRICT warning, 5.1.3之後則不會產生此錯誤訊息。

var是早期php 4、5使用的宣告變數。  至於現在PHP5之後的var,都相等於宣告為public

Class 存取方式

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property

Static properties are accessed by using the :: (Double Colon):self::$property

 

雙引號與單引號的差異, 

"    "  => \  有特殊意義,如要表示單純  \   則使用 \\  跳脫字元

序列 含义
\n 换行(ASCII 字符集中的 LF 或 0x0A (10))
\r 回车(ASCII 字符集中的 CR 或 0x0D (13))
\t 水平制表符(ASCII 字符集中的 HT 或 0x09 (9))
\v 垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))(自 PHP 5.2.5 起)
\e Escape(ASCII 字符集中的 ESC 或 0x1B (27))(自 PHP 5.4.0 起)
\f 换页(ASCII 字符集中的 FF 或 0x0C (12))(自 PHP 5.2.5 起)
\\ 反斜线
\$ 美元标记
\" 双引号
\[0-7]{1,3} 符合该正则表达式序列的是一个以八进制方式来表达的字符
\x[0-9A-Fa-f]{1,2} 符合该正则表达式序列的是一个以十六进制方式来表达的字符

from php manual

 

'    '  => \  無特殊意義

 

nowdoc

無法解析字串中的文字

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.

EOD;

heredoc

可以解析字串中的文字

echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some 
{$foo->bar[1]}.
This should print a capital 'A': \x41

EOT;

// 有效,只有通过花括号语法才能正确解析带引号的键名
echo "This works: {$arr['key']}";

顯示所有的錯誤

error_reporting(E_ALL);

 

// 取得字符串的第一个字符

$str 'This is a test.';
$first $str[0];

 

字符串可以用 '.'(点)运算符连接起来,注意 '+'(加号)运算符没有这个功能   !!!

通常我都使用 .  來連接字串,但其實沒有效率

$outstr = 'literal' . $n . $data . $int . $data . $float . $n; 
67048ms (38.2% slower) 
推薦使用 "" 外加$參數 較快
$outstr = "literal$n$data$int$data$float$n"; 
49058ms (1.15% slower) 


$outstr = "literal{$n}{$data}{$int}{$data}{$float}{$n}" 
49221ms (1.49% slower) 

$outstr = "literal${n}${data}${int}${data}${float}${n}" 

48500ms (fastest

Put your variables in "This is a {$variable} notation"

 

[var1] => 1 
    [ * var2] => 2 
    [ test var3] => 3 

 pulic

* protected 

test private

 

早期的php5之前,針對private 和protected 都教不嚴格,導致上述方法可以取出變數資料,後來在PHP5.1.0之後就有更動,

例如is_callable 就沒有support for private and protected method了  http://bugs.php.net/29210

 

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

static attribute不能用 object存取,但是static method可以

這個還蠻特別的,

class A{

static $var

public static function hello() {

    echo 'hello world!'

  }

}

向上面說的就會變成下面這樣

$a = new A();

$a->hello()     ---> OK

$a->var  -----> not ok

use A::var  instead.

 

另外static method裡面不能有$this->  因為static要在還沒生成object時,就可以使用了!

 

Warning

In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.

 

Only self::$test  use current class,  or use parent::$test in subclass.

<?php
class a{

static protected 
$test="class a";

public function 
static_test(){

echo static::
$test; // Results class b
echo self::$test; // Results class a

}

}

class 
b extends a{

static protected 
$test="class b";

}


$obj = new b();
$obj->static_test();
?>

 

下面是一段有取的程式碼,表示static的特性,$a new出兩次的class,測試看attributes是否被覆蓋掉。

結果顯示staticParent的$parent_only被複寫掉。  其實代表當 current class的__construct方法內,有複寫屬性的code,會先在當前Class尋找是否有該屬性。如果有就不繼續搜尋,如果沒有則往parent找。

舉例: staticchild因為當前的屬性內即有$both_distinct所以不會往上複寫,然而$parent_only 當前class內沒有,所以一定會往parent找該屬性 進行複寫。

這也是和self還有parent的最大差異,self會直接指定當前class的attribute。 parent則直接指定parent的attribute,Static則選擇性先看當前class是否有屬性,沒有才往parent尋找

<?php
declare(strict_types=1);

class 
staticparent {
    static    
$parent_only;
    static    
$both_distinct;
    
    function 
__construct() {
        static::
$parent_only = 'fromparent';
        static::
$both_distinct = 'fromparent';
    }
}

class 
staticchild extends staticparent {
    static    
$child_only;
    static    
$both_distinct;
    
    function 
__construct() {
        static::
$parent_only = 'fromchild';
        static::
$both_distinct = 'fromchild';
        static::
$child_only = 'fromchild';
    }
}


$a = new staticparent;
$a = new staticchild;

echo 
'Parent: parent_only=', staticparent::$parent_only, ', both_distinct=', staticparent::$both_distinct, "<br/>\r\n";
echo 
'Child:  parent_only=', staticchild::$parent_only, ', both_distinct=', staticchild::$both_distinct, ', child_only=', staticchild::$child_only, "<br/>\r\n";
?>

will output:
Parent: parent_only=fromchild, both_distinct=fromparent
Child: parent_only=fromchild, both_distinct=fromchild, child_only=fromchild

 

SuperGlobal的前生,以前都需要使用global,才能在function內使用。PHP 4.1.0之後就不需要拉。

<?php
function test_global()
{
    
// Most predefined variables aren't "super" and require 
    // 'global' to be available to the functions local scope.
    
global $HTTP_POST_VARS;
    
    echo 
$HTTP_POST_VARS['name'];
    
    
// Superglobals are available in any scope and do 
    // not require 'global'. Superglobals are available 
    // as of PHP 4.1.0, and HTTP_POST_VARS is now
    // deemed deprecated.
    
echo $_POST['name'];
}

?>

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

在不久以前,我一直以為這樣子會在每次call test()的時候,把$a設定成0  造成echo結果 只有 0 0 0 0   ((((OMG 現在終於知道了!!!!!!!!

Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

Result:

看到官方一堆老文件,發現有 &method,原來是之前PHP都是 pass by value 現在是pass by reference 當default了。

所以目前來寫php不太會用到&符號哩

function &get_instance_noref()

for php4 , it is fine to use if you need to return a refferenc (example when doing a singleton or a factory).

But for php5 no, as it is not needed in php5 (*cough* php5 use refferences by default)

 

static 不能使用function來assign value;

Static declarations are resolved in compile-time.   不用執行她就跟你報錯了

<?php
function foo(){
    static 
$int 0;          // correct 
    
static $int 1+2;        // correct (as of PHP 5.6)
    
static $int sqrt(121);  // wrong  (as it is a function)

    
$int++;
    echo 
$int;
}

?>

 

static 宣告如果沒有明確指示public protected private則,預設為public 

Constants宣告如果沒有明確指示public protected private則,預設為public 

var是早期php 4、5使用的宣告變數。  至於現在PHP5之後的var,都相等於宣告為public

 

Public 、Protected 、 private的差別

public 所有外部的範圍(outside of CLASS 外面 )都可以使用

Protected: 僅有繼承的class的內部的範圍內才可以使用

private僅有class本身的內部的範圍才可以用

 

何謂內部的範圍

紅色框框就是所謂的內部的範圍,兩個各別的圈圈代表private的範圍

bar extends foo,所以可以在bar 自己的紅色圈圈內呼叫helo() method

,如果是在外面new Bar() 然後 $b->helo()  就超出紅色圈圈範圍了。會報錯誤。

Visibility from other objects 

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

假設傳自己類型的class進去自己的method 可以使用private method,

下面的不行,型態不同。

 

關於 Visibility的某些用途 第一位留言的作者 wbcarts at juno dot com ,解釋的非常棒

http://php.net/manual/en/language.oop5.visibility.php

透過setter 、getter來限制存入的字串型態。

 

Static 是

Late Static Bindings' usage

正如我前面所講的Static會先在當前的class搜尋是否有指定的var or method,沒有才往父類別跑,

以下上圖

test() 呼叫static::who()   ,當透過B class call test(),代表static::who會先在當前B class 內搜尋是否有who method,沒有才往父類別跑

所以結果為 output:  A

這張圖的話則是,B在當前的class搜尋到who方法所以output: B  

 

parent self this還是看這篇好了XDD

http://charleslin74.pixnet.net/blog/post/435851786-%5Bphp%5D-php%E8%A3%A1self%E5%92%8Cthis%E5%8F%8Aparent%E7%9A%84%E5%B7%AE%E5%88%A5

Static的部分看下方連結

https://stackoverflow.com/questions/10504129/when-using-self-parent-static-and-how

Naama Katiee 的說法

The key to the answer is static::who(), remember static:: means you call the method with the actual class (in our case - C).

so C::test() run as follows:

A::foo();   -> calls to A::foo() therefor echo A
parent::foo(); -> calls to C parent (which is B), B::foo() inherits A::foo() which calls to static::who(), but our actual class is C, therefor echo C
self::foo(); -> again calls to foo() which calls to static::who() with our actual class C

或者是參考下面連結,比較完整

http://www.findme.wang/blog/detail/id/240.html

<?php
class {
    public static function 
foo() {
        static::
who();
    }

    public static function 
who() {
        echo 
__CLASS__."\n";
    }
}

class 
extends {
    public static function 
test() {
        
A::foo();
        
parent::foo();
        
self::foo();
    }

    public static function 
who() {
        echo 
__CLASS__."\n";
    }
}
class 
extends {
    public static function 
who() {
        echo 
__CLASS__."\n";
    }
}


C::test();
?>

 

arrow
arrow
    全站熱搜

    蕭瑞文 發表在 痞客邦 留言(0) 人氣()