Properties : class memberphp
Properties : also referred to "attributes" or "fileds"api
declare a property:數組
<1> public private protectedapp
<2> var(treated as public)ide
access to property:ui
<1> non-static: using ->(Object Operator)this
<2> static: using ::spa
string literal:.net
<1>single quotedcode
<2>double quoted
<3>heredoc syntax
<4>nowdoc syntax
1. single quoted(單引號)
to specify a literal single quote, escape it with a backslash(\)
to specify a backslash, double it (\\)
<?php echo 'this is a simple string'; // 能夠錄入多行 echo 'You can also have embedded newlines in strings this way as it is okay to do'; // 輸出: Arnold once said: "I'll be back" echo 'Arnold once said: "I\'ll be back"'; // 輸出: You deleted C:\*.*? echo 'You deleted C:\\*.*?'; // 輸出: You deleted C:\*.*? echo 'You deleted C:\*.*?'; // 輸出: This will not expand: \n a newline echo 'This will not expand: \n a newline'; // 輸出: Variables do not $expand $either echo 'Variables do not $expand $either'; ?>
2. double quoted
3. Heredoc
heredoc syntax: <<<
After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
<?php /* 含有變量的更復雜示例 */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<EOT //idetifiers -> newline My name is "$name". I am printing some $foo->foo. //string itself Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; //same idetifier ?>
output
My name is "MyName". I am printing some Foo. Now, I am printing some Bar2. This should print a capital 'A': A
Heredoc text behaves just like a double-quoted string, without the double quotes
initialize static variables and class properties/constants
<?php // 靜態變量 function foo() { static $bar = <<<LABEL Nothing in here... LABEL; } // 類的常量、屬性 class foo { const BAR = <<<FOOBAR Constant example FOOBAR; public $baz = <<<FOOBAR Property example FOOBAR; } ?>
4. Nowdoc
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings.
A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes,
/* More complex example, with variables. */ class foo { public $foo; public $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<'EOT' My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41 EOT; ?>
output
My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41
Unlike heredocs, nowdocs can be used in any static data context. The typical example is initializing class properties or constants:
Variable parsing
double quotes and heredoc, varible are parsed within it
It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.
Simple synta
<?php $juices = array("apple", "orange", "koolaid1" => "purple"); echo "He drank some $juices[0] juice.".PHP_EOL; echo "He drank some $juices[1] juice.".PHP_EOL; echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won't work echo "He drank some $juices[koolaid1] juice.".PHP_EOL; class people { public $john = "John Smith"; public $jane = "Jane Smith"; public $robert = "Robert Paulsen"; public $smith = "Smith"; } $people = new people(); echo "$people->john drank some $juices[0] juice.".PHP_EOL; echo "$people->john then said hello to $people->jane.".PHP_EOL; echo "$people->john's wife greeted $people->robert.".PHP_EOL; echo "$people->robert greeted the two $people->smiths."; // Won't work ?>
output
He drank some apple juice. He drank some orange juice. He drank some juice made of s. He drank some purple juice. John Smith drank some apple juice. John Smith then said hello to Jane Smith. John Smith's wife greeted Robert Paulsen. Robert Paulsen greeted the two .
Complex syntax {$ or ${
<?php // 顯示全部錯誤 error_reporting(E_ALL); $great = 'fantastic'; // 無效,輸出: This is { fantastic} echo "This is { $great}"; // 有效,輸出: This is fantastic echo "This is {$great}"; echo "This is ${great}"; // 有效 echo "This square is {$square->width}00 centimeters broad."; // 有效,只有經過花括號語法才能正確解析帶引號的鍵名 echo "This works: {$arr['key']}"; // 有效 echo "This works: {$arr[4][3]}"; // 這是錯誤的表達式,由於就象 $foo[bar] 的格式在字符串之外也是錯的同樣。 // 換句話說,只有在 PHP 能找到常量 foo 的前提下才會正常工做;這裏會產生一個 // E_NOTICE (undefined constant) 級別的錯誤。 echo "This is wrong: {$arr[foo][3]}"; // 有效,當在字符串中使用多重數組時,必定要用括號將它括起來 echo "This works: {$arr['foo'][3]}"; // 有效 echo "This works: " . $arr['foo'][3]; echo "This works too: {$obj->values[3]->name}"; echo "This is the value of the var named $name: {${$name}}"; echo "This is the value of the var named by the return value of getName(): {${getName()}}"; echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"; // 無效,輸出: This is the return value of getName(): {getName()} echo "This is the return value of getName(): {getName()}"; ?>