text
stringlengths 1
254
| output
stringlengths 2
255
|
---|---|
If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first.
|
泚æ ã¯ã©ã¹ã¯ã䜿çšãããåã«å®çŸ©ãããŠããå¿
èŠããããŸã! Cart ãæ¡åŒµãã ã¯ã©ã¹ Named_Cart ãäœæãããå Žåã ãŸãã Cart ãå®çŸ©ããå¿
èŠããããŸãã ã¯ã©ã¹ Named_Cart ã«åºã¥ã Yellow_named_cart ãšããååã®ä»ã®ã¯ã©ã¹ã äœæããå ŽåããŸã Named_Cart ãå®çŸ©ãã å¿
èŠããããŸãããŸãšãããšãã¯ã©ã¹ã®å®çŸ©ã®é åºã¯ãéèŠã§ãã
|
The following is valid for PHP 4 only.
|
以äžã®èšè¿°ã¯ãPHP 4ã§ã®ã¿æå¹ã§ãã
|
The:: operator is being used for this.
|
åºåºã¯ã©ã¹ã®é¢æ°ã倿°ãåç
§ãããããŸã ç¹å®ã®ã€ã³ã¹ã¿ã³ã¹ãæã㪠ãã¯ã©ã¹ã®é¢æ°ãåç
§ãããã§ãããšäŸ¿å©ã§ãããããªå ŽåããããŸãã æŒç®å:: ã¯ãã®ãããªå Žåã«äœ¿çšãããŸãã
|
Instead we call example() as a'class function ', that is, as a function of the class itself, not any object of that class.
|
äžã®äŸã§ã¯ã颿°example()ãã¯ã©ã¹Aã§ã³ãŒã«ãããŠããŸãããã¯ã©ã¹A ã®ãªããžã§ã¯ãã¯ãããŸããããã®ããã$a- example()ã®ããã«æžãããš ã¯ã§ããŸããã代ããã«ãexample()ããã¯ã©ã¹é¢æ°ããã€ãŸãããã®ã¯ ã©ã¹ã®ãªããžã§ã¯ãã§ã¯ãªããã¯ã©ã¹èªäœã®é¢æ°ãšããŠãšããŠã³ãŒã«ã㟠ãã
|
In the above example, class B redefines the function example().
|
ã¯ã©ã¹é¢æ°ã¯ãããŸãããã¯ã©ã¹å€æ°ã¯ãããŸãããå®éãã³ãŒã«ããã æç¹ã§ã¯ãªããžã§ã¯ãã¯ååšããŸãããã€ãŸããã¯ã©ã¹é¢æ°ã¯ã©ã®ãªããžã§ ã¯ã倿°ã䜿çšããããšã¯ã§ããŸããã(ããããããŒã«ã«å€æ°ãã°ã㌠ãã«å€æ°ã¯äœ¿çšå¯èœã§ãã)ãŸãã$this ã䜿çšããããšã¯ã§ããŸããã
|
Write A::example() to do this (in fact, you should be writing parent::example(), as shown in the next section).
|
äžã®äŸã§ã¯ãã¯ã©ã¹Bã¯é¢æ°example()ãåå®çŸ©ããŠããŸããã¯ã©ã¹Aã®å
ã®å®çŸ©ã¯é ããã::æŒç®åã䜿çšããŠã¯ã©ã¹Aã®example()ã®å®è£
ãæç€ºç ã«åç
§ããªãéãå©çšã§ããªããªã£ãŠããŸãããããè¡ãã«ã¯ã A::example()ãšæžããŠäžããã(å®éã«ã¯ã次ç¯ã§ç€ºãããã« parent::example()ãšæžãå¿
èŠããããŸãã)
|
Thus, when used from WITHIN an object function, you may use $this and object variables.
|
ãã®ç¶æ³ã§ã¯ãã«ã¬ã³ãã®ãªããžã§ã¯ããããããªããžã§ã¯ã倿°ãä¿æ ããããšãå¯èœã§ããã€ãŸãããªããžã§ã¯ã颿°ã®å
éšã§äœ¿çšãããå Žåã $this ããã³ãªããžã§ã¯ã倿°ã䜿çšããããšãå¯èœã§ãã
|
This is particularly true if your derived class is a refinement or specialisation of code in your base class.
|
åºåºã¯ã©ã¹ã®å€æ°ãšé¢æ°ãåç
§ããã³ãŒããæžãããšãå¯èœã§ããããã¯ã 掟çã¯ã©ã¹ãåºåºã¯ã©ã¹ã®ã³ãŒããç¹å®ã®çšéåãã«æ¹é ãããããå Žå ã«ã¯ãç¹ã«æçšã§ãã
|
For example:
|
PHP ã¯ã'C', 'C++' ããã³ Unix ã·ã§ã«åã®ã³ã¡ã³ãããµããŒãããŸãã äŸãã°ã
|
The "one-line" comment styles actually only comment to the end of the line or the current block of PHP code, whichever comes first.
|
"äžè¡"ã³ã¡ã³ãã¯ãæ¹è¡ãŸãã¯PHPã³ãŒãã®ãããã¯ã®çµãã ã®ãã¡ã©ã¡ããæåã«ããæ¹ãŸã§ã§ãã
|
You should be careful not to nest 'C' style comments, which can happen when commenting out large blocks.
|
'C'åã®ã³ã¡ã³ãããã¹ãããªãããã«æ³šæããå¿
èŠããããŸãã ãã¹ãã¯ã倧ããªãããã¯ãã³ã¡ã³ãã¢ãŠãããéã«çããŸãã
|
The one-line comment styles actually only comment to the end of the line or the current block of PHP code, whichever comes first.
|
1è¡åäœã®ã³ã¡ã³ã圢åŒã¯ãå®éã«ã¯ãæ¹è¡ãŸãã¯PHPã³ãŒããããã¯ã® çµããã®ã©ã¡ããæåã«æ¥ãæ¹ãŸã§ãã³ã¡ã³ããšããŸãã ããã¯ã//? ã®åŸã«HTMLã³ãŒããæ¥ããšè¡šç€ºãããããšãæå³ããŸãã? ã¯ãPHPã¢ãŒããã¹ãããããHTMLã¢ãŒãã«æ»ããŸãã // ã¯åœ±é¿ãäžããŸããã
|
The parser then executes all the code it finds, up until it runs into a PHP closing tag, which tells the parser to just start passing the text through again.
|
PHPã¯ãã¡ã€ã«ããPHPã³ãŒãã®éå§ã瀺ãç¹å¥ãªã¿ã°ãèŠã€ãããŸã§ã¯ åçŽã«ãã®å
容ãåºåããŸããPHPã³ãŒãã®éå§ã瀺ãã¿ã°ãèŠã€ãããš PHPã¯ã³ãŒãã®çµäºã瀺ãã¿ã°ãšã®éã«ããã³ãŒããå®è¡ããŸãããã® ã¡ã«ããºã ã«ããPHPã³ãŒããHTMLã®äžã«åã蟌ãããšãåºæ¥ãã®ã§ãã ã€ãŸããPHPã¿ã°ã®äžã«ããæååã¯PHPã³ãŒããšããŠè§£éãããŸããã å€ã«ããæåå(ã€ãŸãæ®éã®HTML)ã¯ãããŸã§åçŽã«åºåãããã ã ãšããããšã§ãã
|
The tags supported by PHP are:
|
PHPã§ãµããŒããããã¿ã°ã¯ä»¥äžã®ãšããã§ã:
|
2.? echo ("this is the simplest, an SGML processing instruction\n");? ?= expression?
|
äŸ 6-1HTMLããã®è±åºæ³
|
The first way, ?php...?, is the preferred method, as it allows the use of PHP in XML-conformant code such as XHTML.
|
æåã®æ¹æ³ã¯ãççž®åã®ã¿ã°ãæå¹ãªå Žåã«ã®ã¿å©çšå¯èœã§ãã ççž®åã®ã¿ã°ã¯ã short_tags() 颿°ã䜿çšããã (PHP 3ã®ã¿)ã PHPã®èšå®ãã¡ã€ã«ã« short_open_tag ãèšå®ãã ã--enable-short-tags ãªãã·ã§ã³ã configure ã« æå®ã㊠PHP ãã³ã³ãã€ã«ããããšã«ãããæå¹ã«ã§ããŸãã php.ini-distã§ã¯ãããã©ã«ãã§ãã®ãªãã·ã§ã³ã¯æå¹ãšãªã£ãŠããŸããã ç瞮圢ã®ã¿ã°ã®äœ¿çšã¯æšå¥šãããŸããã
|
This can be done via the short_tags() function (PHP 3 only), by enabling the short_open_tag configuration setting in the PHP config file, or by compiling PHP with the --enable-short-tags option to configure.
|
4çªç®ã®æ¹æ³ã¯ãASPååŒã®ã¿ã°ã asp_tags ã®èšå®ã«ããæå¹ãšãªã£ ãŠããå Žåã«ã®ã¿äœ¿çšå¯èœã§ãã
|
The fourth way is only available if ASP-style tags have been enabled using the asp_tags configuration setting.
|
泚æ ASP åã®ã¿ã°ã®ãµããŒãã¯ã3.0.4ã§è¿œå ãããŸããã
|
For portable, redistributable code, be sure not to use short tags.
|
泚æ åå©çšããããããŸãã¯ãèªåã®å¶åŸ¡äžã«ãªãPHPãµãŒãã§éçšããã ã¢ããªã±ãŒã·ã§ã³ãŸãã¯ã©ã€ãã©ãªãéçºããå Žåãççž®åã®ã¿ã°ã® 䜿çšã¯é¿ããã¹ãã§ããããã¯ãççž®åã®ã¿ã°ãã¿ãŒã²ãããµãŒã㌠ã§ãµããŒããããŠããªãå¯èœæ§ãããããã§ãã 坿¬æ§ã®ãããåé
åžå¯èœãªã³ãŒãã§ã¯ãççž®åã®ã¿ã°ã䜿çšããªã ããã«ããŠäžããã
|
Also, the closing tag automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
|
ãããã¯ã®çµäºã¿ã°ã¯ãçŽåŸã«æ¹è¡ãããå Žåããããå«ã ã ãã®ã«ãªããŸãããŸããçµäºã¿ã°ã«ã¯èªåçã«ã»ãã³ãã³ãå«ãŸã㊠ãããšèªèãããŸããåŸã£ãŠPHPã³ãŒãã®æçµè¡ ã«ã¯ã»ãã³ãã³ãèšè¿°ããå¿
èŠã¯ãããŸããã
|
Example 6-2.
|
PHP ã¯ã次ã®ãããªæ§é ã䜿çšå¯èœã§ãã
|
?php if ($expression) {? strong This is true. / strong ?php} else {? strong This is false. / strong ?php}?
|
äŸ 6-2é«åºŠãªãšã¹ã±ãŒãåŠç
|
Instructions are separated the same as in C or Perl - terminate each statement with a semicolon.
|
åœä»€ã¯ãC ã Perl ãšåæ§ã«åé¢ãããŸããã€ãŸããåæã¯ã»ãã³ãã³ã§ çµäºããŸãã
|
?php echo "This is a test";? ?php echo "This is a test"?
|
éããã¿ã° (?) ãæã®çµããã®æå³ãæããŸãããã®ããã以äžã® æã¯ç䟡ã§ãã
|
As the name suggests, that value cannot change during the execution of the script (except the magic constants As the name suggests, that value cannot change during the execution of the script (except the magic constants which aren't actually constants).
|
宿°ã¯ç°¡åãªå€ã®ããã®ID(åå)ã§ãããã®ååã瀺ãããã«ã宿°ã®å€ ã¯ã¹ã¯ãªããå®è¡äžã«å€æŽã§ããŸããã(ç¹å¥ãªå®æ° __FILE__ ããã³ __LINE__ ã¯äŸå€ã§ãã) ããã©ã«ãã§å®æ°ã§ã¯å€§æåå°æåãåºå¥ããŸããæ
£ç¿çã«å®æ°ã¯åžžã«å€§ æåã§è¡šèšãããŸãã
|
As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff ]*
|
宿°ã®ååã¯ãPHPã®ã©ãã«ãšåãèŠåã«åŸããŸããæå¹ãªå®æ°ã®ååã¯ã æåãŸãã¯ã¢ã³ããŒã¹ã³ã¢ã§å§ãŸããä»»æã®æ°ã®æåãæ°åãã¢ã³ããŒã¹ ã³ã¢ãåŸã«ç¶ããŸããæ£èŠè¡šçŸã§ç€ºããšã次ã®ããã«ãªããŸãã [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
|
For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).
|
æ³šæ æ¬ç¯ã®ç®çã«ãããŠã¯ãæå㯠a-z, A-Z, ããã³127ãã255ãŸã§ (0x7f-0xff)ã®ASCIIæåãæããŸãã
|
You can define a constant by using the define() -function.
|
宿°ã®ã¹ã³ãŒãã¯ã°ããŒãã«ã§ããã€ãŸããã¹ã³ãŒãã«ãããã¹ã¯ãªãã ã®äžã§ã¯ã©ãã§ã宿°ã«ã¢ã¯ã»ã¹ã§ããŸãã
|
Only scalar data (boolean, integer, float and string) can be contained in constants.
|
define() 颿°ã䜿çšããããšã«ããã宿°ãå®çŸ©ã ãããšãå¯èœã§ãã宿°ãäžåºŠå®çŸ©ããããšã倿ŽãŸãã¯æªå®çŸ©ãšããã ãšã¯ã§ããŸããã
|
Unlike with variables, you should not prepend a constant with a $.
|
ã¹ã«ã©ãŒããŒã¿( boolean, integer, double, string) ã®ã¿ã宿°ã®äžã«å«ãããš ãå¯èœã§ãã
|
Use get_defined_constants() to get a list of all defined constants.
|
åã«å®æ°ã®ååãæå®ããããšã«ããããã®å€ãåŸãããšãå¯èœã§ãã 倿°ãšã¯ç°ãªãããã®åã« $ã¯äžèŠã§ãã 宿°ã®ååãåçã«åŸãå¿
èŠãããå Žåã宿°ã®å€ãèªãããã«é¢æ° constant() ã䜿çšããããšãå¯èœã§ãã å®çŸ©æžã®å®æ°ã®äžèЧãåŸãã«ã¯ã get_defined_constants() ã䜿çšããŠäžããã
|
Constants and (global) variables are in a different namespace.
|
泚æ 宿°ãš(ã°ããŒãã«)倿°ã¯ãç°ãªãåå空éã«ãããŸãã äŸãã°ã TRUE ãš $TRUE ã¯éããã®ãæå³ããŸãã
|
If you use an undefined constant, PHP assumes that you mean the name of the constant itself.
|
æªå®çŸ©ã®å®æ°ã䜿çšããå ŽåãPHPã¯ãã®å®æ°èªäœã®ååã䜿çšãããšä»® å®ããŸãããã®éã notice ãçºçããŸãã ãã宿°ãèšå®ãããŠãããã©ãããç¥ãã«ã¯ã defined() 颿°ã䜿çšããŠäžããã
|
Use the defined() -function if you want to know if a constant is set.
|
倿°ãšã®éãã¯æ¬¡ã®ããã«ãªããŸãã
|
Constants do not have a dollar sign ($) before them;
|
宿°ã¯ãåã«ãã«èšå·( $)ãèŠããŸããã
|
Constants may be defined and accessed anywhere without regard to variable scoping rules;
|
宿°ãå®çŸ©ããããšãã§ããã®ã¯ã define() 颿° ã®ã¿ã§ããåãªã代å
¥ã«ããå®çŸ©ã¯ã§ããŸããã
|
Constants may only evaluate to scalar values.
|
宿°ã¯ãå®çŸ©ããããšãã§ãã倿°ã®ã¹ã³ãŒãèŠåã«é¢ä¿ãªãããã ããå Žæããã¢ã¯ã»ã¹å¯èœã§ãã
|
?php define( "CONSTANT", "Hello world ."); echo CONSTANT; / / outputs "Hello world. "echo Constant; / / outputs "Constant" and issues a notice.?
|
宿°ã¯äžåºŠèšå®ããããšåå®çŸ©ãŸãã¯æªå®çŸ©ãšããããšã¯ã§ããŸããã
|
Variables from outside PHP
|
宿°ã¯ãã¹ã«ã©ãŒå€ãšããŠã®ã¿è©äŸ¡å¯èœã§ãã
|
Predefined constants
|
äŸ 9-1宿°ã®å®çŸ©
|
Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.
|
PHPã«ã¯å®è¡ãããã¹ã¯ãªããã§äœ¿çšå¯èœãªå€ãã®å®çŸ©æžã¿ã®å®æ°ããã㟠ãããããããããã®å®æ°ã®å€ãã¯ãçš®ã
ã®æ¡åŒµã¢ãžã¥ãŒã«ã«ããäœæã ããåçãªããŒããã³ã³ãã€ã«æã®çµèŸŒã¿ã«ããããããã®æ¡åŒµã¢ãžã¥ãŒ ã«ã䜿çšå¯èœã§ããå Žåã«ã®ã¿å®çŸ©ãããŸãã
|
For example, the value of __LINE__ depends on the line that it 's used on in your script.
|
å®çŸ©æžã¿ã®å®æ°ã®äžèЧã¯ã å®çŸ©æžã® 宿° ã®ç¯ã§åç
§ã§ããŸãã
|
After this assignment, you' d expect $a's value to be 5 as well, so if you wrote $b = $a, you'd expect it to behave just as if you wrote $b = 5.
|
åŒã¯ãPHP ã«ãããæãéèŠãªåºç€ç³ã§ããPHPã«ãããŠã¯ã ã»ãšãã©å
šãŠã®ãã®ã¯åŒã§èšè¿°ãããŸãã æãç°¡åã§æãæ£ç¢ºãªåŒã®å®çŸ©ã¯ã"å
šãŠã®åŒã«ã¯å€ãããã" ã§ãã
|
Functions are expressions with the value of their return value.
|
èããããç°¡åãªäŸã¯ã宿°ãšå€æ°ã§ãã "$a = 5" ãšå
¥åãããšã$a ã« '5' ã代å
¥ããããšã«ãªããŸãã '5' ã¯ãæããã«ã 5 ãšããå€ã§ãã èšèãå€ãããš '5' 㯠5 ãšããå€ãæããåŒãªã®ã§ãã (ãã®å Žåã'5' ã¯æŽæ°å®æ°ã§ãã)
|
PHP supports three scalar value types: integer values, floating point values and string values (scalar values are values that you can't'break 'into smaller pieces, unlike arrays, for instance).
|
ãã®ä»£å
¥ã®åŸã$a ã®å€ã¯ã5 ã§ããããšãäºæ³ãããŸãã ãã£ãŠã$b = $a ãšæžããå Žåã$b = 5 ãšæžããã®ãš åãããã«åäœããããšãäºæ³ãããŸãã èšãæãããš $a 㯠5 ãšããå€ãæã€åŒãªã®ã§ãã å
šãŠãæ£ããåäœããå Žåãäœãèµ·ãããããã®ããšãæ£ç¢ºã«è¡šçŸããŠããŸãã
|
PHP is an expression-oriented language, in the sense that almost everything is an expression.
|
å°ãã ãããè€éãªåŒã®äŸã¯ã颿°ã§ãã äŸãã°ã次ã®é¢æ°ãèããŠã¿ãŸãããã
|
But the truth is that there' s one additional value involved here, and that's the value of the assignment itself.
|
ããªãã颿°ã®æŠå¿µã«æ
£ããŠãããšä»®å®ãããš (ããã§ãªãå Žåã¯ã颿°ã«é¢ããç« ãåç
§äžããã)ã $c = foo() ãšå
¥åããããšã¯ã $c = 5 ãšæžãããšãšæ¬è³ªçã«å
šãåãã§ ãããšäºæ³ããããããããŸããããã®äºæ³ã¯ãæ£ããã§ãã 颿°ã¯ããã®è¿ãå€ãå€ãšããåŒãªã®ã§ãã foo() 㯠5 ãè¿ãã®ã§ãåŒ 'foo()' ã®å€ã¯ 5 ã§ãã éåžžã颿°ã¯ã決ãŸã£ãæ°ã ããè¿ãã®ã§ã¯ãªããäœããèšç®ããŸãã
|
Post-increment, which is written '$variable++' evaluates to the original value of $variable, before it was incremented (PHP increments the variable after reading its value, thus the name'post-increment').
|
ãã¡ãããPHP ã®å€ã¯æŽæ°ã§ããå¿
èŠã¯ãããŸãããã å€ãã®å Žåãããã§ã¯ãããŸããã PHP ã¯ã3 çš®é¡ã®ã¹ã«ã©ãŒå(æŽæ°ãæµ®åå°æ°ç¹æ°ãæåå)ããµããŒãããŸãã (ã¹ã«ã©ãŒãšã¯ãé
åãšããšç°ãªããããå°ããªéšåã«'åå²ãã'ããšã ã§ããªãå€ã®ããšã§ãã) PHP ã¯ã2çš®é¡ã®è€å(éã¹ã«ã©ãŒ)å(é
åãšãªããžã§ã¯ã)ããµããŒãããŸãã ãããã®åã®å€ã¯ã倿°ã«ä»£å
¥ããããšãã§ãã 颿°ããã®è¿ãå€ãšããããšãã§ããŸãã
|
A much more common practice is to write '$a = $a + 3'. '$a + 3' evaluates to the value of $a plus 3, and is assigned back into $a, which results in incrementing $a by 3.
|
æ¯èŒæŒç®åã¯ã極ããŠæšæºçãªåŒã§ã æ¯èŒæŒç®åã¯ã0ã1ã®ã©ã¡ãããå€ãšããŸãã ãã®å€ã®æå³ã¯ã(ããããã) FALSE ãš TRUE ã§ãã PHPã¯ã (倧ãªã)ã =(倧ãªãã€ã³ãŒã«)ã=(ã€ã³ãŒã«)ã (å°ãªã)ã =(å°ãªãã€ã³ãŒã«)ããµããŒãããŸãã ãããã®åŒã¯ã if æã®ãããªæ¡ä»¶åŒã®å
éšã§äžè¬ç㫠䜿çšãããŸãã
|
In addition to being shorter and clearer, this also results in faster execution.
|
ããäžçš®é¡ãterniary æ¡ä»¶æãšããåŒããããŸããã ä»ã®èšèªã§èŠãããšããªãå Žåã«ã¯çè§£ã§ããªããããããŸããã
|
Notice that it is NOT 3, but the combined value of $a plus 3 (this is the value that's assigned into $a).
|
次ã®äŸã¯ãå眮ããã³åŸçœ®å ç®åãšå€å°äžè¬çãªåŒã®çè§£ã å©ããŠãããããšã§ãããã
|
There is one more expression that may seem odd if you haven 't seen it in other languages, the ternary conditional operator:
|
æ¬ç« ã®ã¯ããã§ãæ§ã
ãªæã®åã«ã€ããŠæžããšèšããŸãããã çŽæããããã«ãåŒã¯ãæãªã®ã§ãã ããããå
šãŠã®åŒãã²ãšã€ã®æãªããã§ã¯ãããŸããã ãã®å Žåãæã¯ã'åŒ'';' ãã€ãŸãåŒã®åŸã«ã»ãã³ãã³ãã€ãã圢åŒã§ãã '$b=$a=5' ã«ãããŠã$a=5 ã¯æå¹ãªåŒã§ãããèªèº«ãå€ãšããæã§ã¯ ãããŸããããããã'$b=$a=5;' ã¯æå¹ãªæã§ãã
|
In the beginning of the chapter we said that we 'll be describing the various statement types, and as promised, expressions can be statements.
|
æåŸã«ãæçãªäºé
ãšããŠåŒã®è«çå€ã«ã€ããŠèª¬æããŸãã å€ãã®ã€ãã³ããäž»ã«æ¡ä»¶ä»ãå®è¡ãšã«ãŒãã«ãããŠã åŒã®ç¹å®ãªå€ã«ã¯é¢å¿ããªããã TRUE ãŸã㯠FALSE ã®ã©ã¡ãã æå³ãããã«é¢å¿ããããšããããšããããŸãã 宿° TRUE ãš FALSE (倧æåå°æåãåºå¥ããªã)ã¯ãè«çåã®å€ããšãåŸã å€ã§ããå¿
èŠã«å¿ããŠåŒã¯è«çå€ã«å€æãããŸããè©³çŽ°ãªææ³ã«ã€ããŠã¯ã åãã£ã¹ãã«é¢ããã»ã¯ã·ã§ã³ ãåç
§ãã ããã
|
The constants TRUE and FALSE The constants TRUE and FALSE (case-insensitive) are the two possible boolean values.
|
PHP ã¯ãå®å
šã§åŒ·åãªåŒã®å®è£
ãæäŸããŸãã ãããå®å
šã«èšè¿°ããããšã¯ããã®ããã¥ã¢ã«ã®ç¯å²ãè¶
ããŠããŸãã äžèšã®äŸã¯ãåŒãšã¯äœãããããŠã䟿å©ãªåŒãã©ããã£ãŠäœãããšããããš ã«é¢ããŠè¯ãã¢ã€ãã¢ãäžããã«éããããŸããã æ¬ããã¥ã¢ã«ã®æ®ãã®éšåã§ã¯ expr ãšãã ããŒã¯ã䜿çšããŸããããã㯠PHP ã®æå¹ãªåŒãæå³ããŸãã
|
In PHP 4, a function becomes a constructor, when it has the same name as the class it is defined in - the difference is subtle, but crucial (see below).
|
PHP 3 ãš PHP 4 ã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ã®åäœã¯ç°ãªããŸããPHP 4ã®å® çŸ©ã®æ¹ãæããã«å¥œãŸãããšèšããŸãã
|
This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "new ".
|
ã³ã³ã¹ãã©ã¯ã¿ã¯ã new ã«ããã¯ã©ã¹ã®æ°ããã€ã³ã¹ ã¿ã³ã¹ãäœæããéã«èªåçã«ã³ãŒã«ãããã¯ã©ã¹é¢æ°ã§ãã PHP 3ã§ã¯ããã颿°ããã¯ã©ã¹åãšåãååãæããŠããå Žåã«ã³ã³ã¹ ãã©ã¯ã¿ã«ãªããŸãããPHP 4ã§ã¯ããã颿°ãããã®ã³ã³ã¹ãã©ã¯ã¿ã å®çŸ©ãããŠããã¯ã©ã¹ãšåãååãæããŠããå Žåã«ã³ã³ã¹ãã©ã¯ã¿ã«ãª ããŸãããããã®éãã¯å
ãã§ãããéèŠãªéãããããŸãã(以äžãå ç
§)
|
The following examples should be read carefully to understand these limitations.
|
@new ã®ããã«ã³ã³ã¹ãã©ã¯ã¿ã§çºçãããšã©ãŒã® åºåãæå¶ããããã« @ æŒç®åã䜿çšããããšã å¯èœã§ãã
|
In PHP 3, no constructor is being called in the above example.
|
PHP 3ã§ã¯ã掟çã¯ã©ã¹ãšã³ã³ã¹ãã©ã¯ã¿ã«ã¯è€æ°ã®å¶çŽããããŸããã ããã®å¶éãçè§£ããããã«ä»¥äžã®äŸã泚æããŠåŸ¡èЧäžããã
|
The name of the class is B, and there is no function called B() in class B.
|
PHP 3ã§ã¯ãäžã®äŸã§ã³ã³ã¹ãã©ã¯ã¿ã¯ã³ãŒã«ãããŸãããPHP 3ã®èŠåã§ ã¯ããã³ã³ã¹ãã©ã¯ã¿ã¯ã¯ã©ã¹ãšåãååãæãã颿°ã§ããããšãªã㟠ããã¯ã©ã¹åã¯Bã§ãããã¯ã©ã¹Bã®äžã«ã¯B()ãšãã颿°ããªããããäœ ããããŸããã
|
The above example would have printed 'I am the constructor of A. br' in PHP 4.
|
ããã¯ãPHP 4ã§ã¯ã次ã®ãããªå¥ã®èŠåãå°å
¥ãããä¿®æ£ãããŸããã ããã¯ã©ã¹ã«ã³ã³ã¹ãã©ã¯ã¿ããªãå Žåãåºåºã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã ããå Žåã«ã¯ã³ãŒã«ãããŸããäžã®äŸã¯ãPHP 4ã§ã¯ã 'Aã®ã³ã³ã¹ãã©ã¯ã¿ã§ã br 'ãšåºåãããŸãã
|
In PHP 3, the function B() in class A will suddenly become a constructor in class B, although it was never intended to be.
|
PHP 3ã§ã¯ãã¯ã©ã¹Aã®é¢æ° B() ã¯æå³ãããŠããªãå Žåã§ããã¯ã©ã¹B ã®ã³ã³ã¹ãã©ã¯ã¿ã«çªç¶ãªã£ãŠããŸããŸãããPHP 3ã®èŠåã¯æ¬¡ã®ããã« ãªããŸãããã³ã³ã¹ãã©ã¯ã¿ã¯ã¯ã©ã¹åãšåãååã®é¢æ°ã§ããããPHP 3ã¯ããã®é¢æ°ãã¯ã©ã¹Bã§å®çŸ©ãããŠããããšããã®é¢æ°ãç¶æ¿ãããŠã ããã©ããã¯èæ
®ããŸããã
|
This is fixed in PHP 4 by modifying the rule to: 'A constructor is a function of the same name as the class it is being defined in.'.
|
ããã¯ãPHP 4ã§ã¯æ¬¡ã®ããã«èŠåãä¿®æ£ãããŸããããã³ã³ã¹ãã©ã¯ã¿ ã¯ããããå®çŸ©ãããŠããã¯ã©ã¹ãšåãååã®é¢æ°ã§ãããã€ãŸããPHP 4ã§ã¯ãã¯ã©ã¹Bãç¬èªã®ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãæããªãå Žåã«ã¯ãåºåºã¯ ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãã³ãŒã«ããããAã®ã³ã³ã¹ãã©ã¯ã¿ã§ã br ã ãšåºåãããŸãã
|
Caution
|
PHP 3 ããã³ PHP 4ã®ã©ã¡ãã§ã掟çã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ããåºåº ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãèªåçã«ã³ãŒã«ããããšã¯ã§ããŸããã
|
It is your responsibility to propagate the call to constructors upstream where appropriate.
|
泚æ PHP 3ããã³PHP 4ã«ã¯ããã¹ãã©ã¯ã¿ã¯ãããŸããããã¹ãã©ã¯ã¿ã®æ© èœã®å€ããã·ãã¥ã¬ãŒã·ã§ã³ããã«ã¯ã代ããã« register_shutdown_function() ã䜿çšããŸãã
|
Destructors are functions that are called automatically when an object is destroyed, either with unset() or by simply going out of scope.
|
ãã¹ãã©ã¯ã¿ã¯ã unset() ãŸãã¯ã¹ã³ãŒããã ã§ãããšã«ããããªããžã§ã¯ããç Žæ£ããã床ã«èªåçã«ã³ãŒã«ããã颿°ã§ãã PHPã«ã¯ãã¹ãã©ã¯ã¿ã¯ãããŸããã
|
Caution
|
ã¯ã©ã¹ã¯ã倿°ããã³ãããã®å€æ°ã§åäœãã颿°ã®éãŸãã§ãã ã¯ã©ã¹ã¯æ¬¡ã®ãããªæ§æã«ããå®çŸ©ãããŸãã
|
You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.
|
ãã®äŸã¯ãç± ã®äžã®ç©ãšç± ã«ã¢ã€ãã ãå ãããé€ãããããäºã€ã®é¢æ°ãžã® 飿³é
åãããªã Cart ãšããååã®ã¯ã©ã¹ãå®çŸ©ããŸãã
|
It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
|
以äžã®æ³šææžãã¯PHP 4ã«é¢ãããã®ã§ãã
|
To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class.
|
åå stdClass ã¯ãZendã«ããå
éšçã«äœ¿çšãããä¿ åãããŠããŸããPHPã§ stdClass ãšããååã®ã¯ã© ã¹ã䜿çšããããšã¯ã§ããŸããã
|
Classes are types, that is, they are blueprints for actual variables.
|
颿° __sleep ããã³ __wakeup ã¯ã PHPã¯ã©ã¹å
ã§äœ¿çšãããç¹æ®ãªé¢æ°ã§ãããããã®é¢æ°ã«ä»éããç¹æ® ãªæ©èœã䜿çšããå Žå以å€ã¯ãã¯ã©ã¹å
ã§ãããã®ååãä»ãã颿°ã 䜿çšããããšã¯ã§ããŸãããè©³çŽ°ãªæ
å ±ã«ã€ããŠã¯ã以äžãåç
§äžããã
|
This creates the objects $cart and $another_cart, both of the class Cart.
|
PHP ã¯ã__ã§å§ãŸãå
šãŠã®é¢æ°åãç¹æ®ãªé¢æ°ãšããŠäºçŽããŠããŸãã ããã¥ã¡ã³ãã«èšèŒãããç¹æ®é¢æ°ã®æ©èœã䜿çšããå Žå以å€ã¯ã__ã æãã颿°åã䜿çšããªãããšãæšå¥šããŸãã
|
Just like with directories where you 'll have to type the full pathname in order to reach each file from the toplevel directory, you have to specify the complete name of the function you want to call:
|
泚æ PHP 4ã§ã¯ã倿° var ã«ã€ããŠã¯å®æ°ã«ããåæåã® ã¿ãå¯èœã§ãã宿°ä»¥å€ã§åæåãè¡ãå Žåã«ã¯åæå颿°ãå¿
èŠã§ãã ãã®åæå颿°ã¯ããªããžã§ã¯ããã¯ã©ã¹ããæ§ç¯ãããéã«èªåçã« ã³ãŒã«ãããŸãããã®ãããªé¢æ°ã¯ã³ã³ã¹ãã©ã¯ã¿ãšåŒã°ããŸãã(ä»¥äž åç
§)
|
Thus, the names $cart - items and $another_cart - items name two different variables.
|
ã¯ã©ã¹ã¯åãã€ãŸããå®éã®å€æ°ã®éåã§ãã new æŒ ç®åã«ããææã®åã®å€æ°ãäœæããå¿
èŠããããŸãã
|
You might want to take a look at the Class / Object Functions
|
泚æ ã¯ã©ã¹ãšãªããžã§ã¯ããåŠçããåªãã颿°ãããã€ããããŸãã ã¯ã©ã¹/ãªããžã§ã¯ã颿° ããã£ãšã¿ãŠã¿ããšè¯ãã§ãããã
|
If so, that function is being run prior to any serialization.
|
serialize() ã¯ãã¯ã©ã¹ã«ç¹æ®ãªåå __sleep ãæãã颿°ããããã©ããã調ã¹ãŸããã ããããå Žåããã®é¢æ°ã¯ã·ãªã¢ã«åã®åã«å®è¡ãããŸãããã®é¢æ°ã«ã ããªããžã§ã¯ããã¯ãªã¢ããã·ãªã¢ã«åãããªããžã§ã¯ãã®å
šãŠã®å€æ°ã® ååãæããé
åãè¿ãããšãåæãšãããŸãã
|
The intended use of __sleep is to close any database connections that object may have, committing pending data or perform similar cleanup tasks.
|
å
žåç㪠__sleep ã®äœ¿çšæ³ã¯ããªããžã§ã¯ããæã ãå
šãŠã®ããŒã¿ããŒã¹æ¥ç¶ãéããããéäžã®ããŒã¿ãã³ããããããã 䌌ããããªã¿ã¹ã¯ã®ã¯ãªã¢ãè¡ããšãã£ããã®ã§ãããŸãããªããžã§ã¯ã ãéåžžã«å€§ããããã€ãå®å
šã«ä¿åããå¿
èŠããªãå Žåããã®é¢æ°ãæçš ã§ãã
|
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup.
|
éã«ã unserialize() ã¯ãç¹æ®ãªåå __wakeup ãæãã颿°ãååšãããã©ããã確èªã ãŸãããããååšããå Žåããã®é¢æ°ã¯ããã®ãªããžã§ã¯ããæããå
šãŠ ã®ãªãœãŒã¹ãåæ§ç¯ããããšãå¯èœã§ãã
|
The intended use of __wakeup is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.
|
å
žåç㪠__wakeup ã®äœ¿çšæ³ã¯ãã·ãªã¢ã«åã«ãã倱㣠ãããŒã¿ããŒã¹æ¥ç¶ãå
šãŠå床埩æ§ãããããã®ä»ã®ååæåäœæ¥ãå®è¡ ããããšãã£ããã®ã§ãã
|
This is because "new" does not return a reference by default, instead it returns a copy.
|
ã³ããŒæŒç®å = ã«ããäœæããã $bar1 ãš åç
§æŒç®å = ã«ããäœæããã $bar2 ã®éã®å·®ç°ããããã©ãã ã確èªããŠã¿ãŸãããã
|
Another final example, try to understand it.
|
泚æ (PHP 4以éã§ã¯ãªãã¡ã¬ã³ã¹ã«ãŠã³ãã£ã³ã°ã䜿çšããŠããããã)å ç
§ã§ã¯ãªãã³ããŒãè¿ãããšã§æ§èœãäœäžããããšã¯ãããŸãããéã« å€ãã®å Žåãåç
§ã䜿ããããåçŽã«ã³ããŒã䜿ã£ãæ¹ãè¯ãçµæãšãª ããŸããããã¯ãåç
§ã®äœæã«ã¯æéãããããŸãããã³ããŒã®äœæã« ã¯çæ³çã«ã¯æéãå
šãããããªãããã§ãã(ãã ãã倧ããªé
å㟠ãã¯ãªããžã§ã¯ãã§ãã®äžã€ã倿Žããããšã次ã
ã«åç
§å
ã®ä»ã®èŠçŽ ã«åç
§å
ã«æ³¢åãããšãã£ãå Žåãé€ããŸã)
|
The magic functions __sleep and __wakeup
|
æåŸã«å¥ã®äŸã«ã€ããŠèããŠã¿ãŠäžããã
|
The resulting variable is of type object, but has no class and no methods, thus it is pretty useless (it has become just like an array with a funny syntax).
|
泚æ PHP 3ã§ã¯ããªããžã§ã¯ãã¯ã·ãªã¢ã«åãéã·ãªã¢ã«åã®èª²çšã§ã¯ã©ã¹ã® çžé¢ã倱ã£ãŠããŸããŸããã埩å
ããã倿°ã¯ããªããžã§ã¯ãåã§ããã ã¯ã©ã¹ãã¡ãœããããããŸããããã®ãããå
šãå®çšçã§ã¯ãããŸãã ã§ããã(å€ãã£ãæ§æã®é
åã®ããã«ãªã£ãŠããŸããã)
|
Using serialize to save an object will save all variables in an object.
|
以äžã®æ
å ±ã¯ãPHP 4ã§ã®ã¿æå¹ã§ãã
|
If you want to be able to unserialize this on page2.php, recreating $a of class A, the definition of class A must be present in page2.php.
|
serialize() ã¯ãPHPã§ä¿åå¯èœãªå
šãŠã®å€ã®ãã€ã ã¹ããªãŒã 衚çŸãæããæååãè¿ããŸãã unserialize() ã¯ããã®æååã䜿çšããŠå
ã®å€æ° å€ãåçããããšãå¯èœã§ãããªããžã§ã¯ããä¿åããããã«ã·ãªã¢ã«å ãè¡ããšããªããžã§ã¯ãã®å
šãŠã®å€æ°ãä¿åãããŸãããªããžã§ã¯ãã®é¢ æ°ã¯ä¿åãããŸãããã¯ã©ã¹åã ããä¿åãããŸãã
|
This basically means that these objects can show up on any of your pages once they become part of your session.
|
ã»ãã·ã§ã³ã䜿çšããŠããå Žåã«ããªããžã§ã¯ããç»é²ããããã« session_register() ã䜿çšãããšããããã®ãªããžã§ ã¯ãã¯åPHPããŒãžã®æåŸã§èªåçã«ã·ãªã¢ã«åãããæ¬¡ã®ããŒãžã§èªå çã«éã·ãªã¢ã«åãããŸããããã¯ãåºæ¬çã«ããããã®ãªããžã§ã¯ãã äžæŠã»ãã·ã§ã³å€æ°ãšãªããšãå
šãŠã®ããŒãžã«çŸããããšãæå³ããŸãã
|
If you don 't and an object is being unserialized without its class definition being present, it will lose its class association and become an object of class stdClass without any functions available at all, that is, it will become quite useless.
|
å
šãŠã®ããŒãžã§ãããã®ã¯ã©ã¹ãå®éã«ã¯äœ¿çšããªãå Žåã§ããå
šãŠã®ã㌠ãžã§ãã®ãããªç»é²ãããå
šãŠã®ãªããžã§ã¯ãã®ã¯ã©ã¹å®çŸ©ãèªã¿èŸŒãã ãšãåŒ·ãæšå¥šãããŸãããããè¡ããã«ãã¯ã©ã¹å®çŸ©ãååšããªãç¶æ
ã§ ãªããžã§ã¯ããéã·ãªã¢ã«åãããå Žåãã¯ã©ã¹ã®çžé¢ã¯å€±ãããå
šãŠã® 颿°ãå©çšã§ããªããªããããã¯ã©ã¹ã®ãªããžã§ã¯ã stdClass ã¯å©çšäŸ¡å€ãããªãäœããªããŸãã
|
Prev
|
ãã®ãããäžã®äŸã§ã session_register("a") ãå®è¡ ããããšã«ãã $a ãã»ãã·ã§ã³ã®äžéšãšãªã£ãå Žåãpage1.php ããã³ page2.phpã ãã§ãªããå
šãŠã®ããŒãžã§ãã¡ã€ã« classa.inc ãèªã¿èŸŒãã¹ãã§ãã
|
These work just like those.
|
åŠæ ¡ã§ç¿ã£ãåºç€ä»£æ°ãæ¶ããŠããŸããïŒ ãã®æŒç®åã¯ããããšåæ§ã«åäœããŸãã
|
Difference of $a and $b.
|
衚 11-2ä»£æ°æŒç®å
|
The division operator ("/") returns a float value anytime, even if the two operands are integers (or strings that get converted to integers).
|
2ã€ã®ãªãã©ã³ããæŽæ°ã®å Žå(ãŸãã¯æŽæ°ã«å€æãããæååã®å Žå)ã§ ãã£ãŠããå²ç®æŒç®å ("/") ã¯åžžã«floatå€ãè¿ããŸãã
|
$a = array( "a "= "apple", "b" = "banana"); $b = array("a "= "pear", "b" = "strawberry", "c "= "cherry"); $c = $a + $b; var_dump($c);
|
PHPã®å¯äžã®æŒç®åã¯ã + æŒç®åã§ãã å³åŽã®é
åãå·ŠåŽã«è¿œå ããŸãããã ããéè€ããããŒã¯äžæžããã ãŸããã
|
The value of an assignment expression is the value assigned.
|
代å
¥æŒç®åã®åºæ¬ãšãªããã®ã¯ "=" ã§ãããã®æŒç®åã«é¢ããŠæåã«æ ãä»ãæå³ã¯"çãã"ã§ãããããããŸãããããããããã§ã¯ãããŸã ããæ¬åœã¯ãå·Šãªãã©ã³ãã«å³ãªãã©ã³ãã®åŒã®å€ãèšå®ãã("åŸãŠä»£å
¥ ãã")ããšãæå³ããŸãã
|
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
|
代å
¥åŒã®å€ã¯ã代å
¥ãããå€ã§ããã€ãŸãã"$a = 3" ã®å€ã¯ã3ã§ãã ããã«ããã以äžã®ãããªããªãããŒãªããšãã§ããããã«ãªããŸãã
|
Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other.
|
åºæ¬ä»£å
¥æŒç®åã«å ããŠãå
šãŠã®ãã€ããªæŒç®åããã³æååæŒç®åã«é¢ ããŠãè€åæŒç®åãããããŸãããã®æŒç®åã«ãããåŒã®å€ã䜿çšããåŒ ã®çµæã«ãã®å€ãèšå®ããããšãã§ããŸããäŸãã°ã
|
?php echo 12 ^ 9; / / Outputs '5 'echo "12 "^ "9"; / / Outputs the Backspace character (ascii 8) / / ('1' (ascii 49)) ^ ('9 '(ascii 57)) = #8 echo "hallo" ^ "hello"; / / Outputs the ascii values #0 #4 #0 #0 #0 / / 'a' ^ 'e '= #4?
|
ãããæŒç®åã¯ãæŽæ°ã«ãããç¹å®ã®ãããããªã³ãŸãã¯ãªãã«ããããšã å¯èœã«ããŸãããã巊蟺å€ãšå³èŸºå€å
±ã«æååã§ãã£ãå Žåã«ã¯ãããæŒç®å㯠æåã«å¯ŸããŠäœçšããŸãã
|
Shift the bits of $a $b steps to the right (each step means "divide by two")
|
衚 11-3ãããæŒç®å
|
Table 11-4.
|
æ¯èŒæŒç®åã¯ããã®ååã瀺ãããã«ãäºã€ã®å€ãæ¯èŒããŸãã
|
TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
|
衚 11-4æ¯èŒæŒç®å
|
Another conditional operator is the "?:" (or ternary) operator, which operates as in C and many other languages.
|
æ¡ä»¶æŒç®åãšããŠã¯ãä»ã« "?:"æŒç®å(ãŸãã¯äžé
æŒç®å)ããããŸãã ãã®æŒç®åã®åäœã¯ãCãŸãã¯ä»ã®å€ãèšèªãšåãã§ãã
|
If the track_errors feature is enabled, any error message generated by the expression will be saved in the variable $php_errormsg.
|
PHPã¯ããšã©ãŒå¶åŸ¡æŒç®å(@)ããµããŒãããŠããŸããPHPã®åŒã®åã«ä»ã ãå Žåããã®åŒã«ããçæããããšã©ãŒã¡ãã»ãŒãžã¯ç¡èŠãããŸãã
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.