Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
becki:linux:php [2010-05-21 09:16] becki |
becki:linux:php [2018-01-30 09:02] (aktuell) becki [Solution] |
||
---|---|---|---|
Zeile 54: | Zeile 54: | ||
</code> | </code> | ||
- | ==== Copying == | + | ==== Copying, Passing and Returning == |
By default, arrays are copied //by value// (Unlike PHP objects and Java arrays and objects): | By default, arrays are copied //by value// (Unlike PHP objects and Java arrays and objects): | ||
+ | |||
+ | === Assignment == | ||
<code php> | <code php> | ||
- | $a = array ('wespe', 'bine', 'hummel'); | + | $a=array(0); |
- | $b = $a; | + | |
- | $c = &$a; | + | $b= $a; /* Deep copy: Assignment by value */ |
- | array_shift($a); // shifts the first value of the array off | + | $b[0]= 1; |
- | echo "b: "; print_r($b); | + | assert($a[0] == 0); |
- | echo "c: "; print_r($c); | + | |
+ | $c= &$a; /* Shallow copy: Assignment by reference */ | ||
+ | $c[0]= 2; | ||
+ | assert($a[0] == 2); | ||
+ | $a[0]= 3; | ||
+ | assert($c[0] == 3); | ||
+ | |||
+ | $d= $c; /* $c is not different from $a: Again assignment by value */ | ||
+ | $d[0]= 4; | ||
+ | assert($a[0] == 3); | ||
+ | assert($c[0] == 3); | ||
</code> | </code> | ||
- | Result (with PHP5): | + | === Passing to and returning from Functions == |
- | <code> | + | |
- | b: Array | + | <code php> |
- | ( | + | function f($v) { $v[0]= 1; } |
- | [0] => wespe | + | function g(&$w) { $w[0]= 2; } |
- | [1] => bine | + | function h(&$x) { $x[0]= 3; return $x; } |
- | [2] => hummel | + | $a= array(0); |
- | ) | + | |
- | c: Array | + | f($a); /* pass-by-value */ |
- | ( | + | assert($a[0] == 0); |
- | [0] => bine | + | |
- | [1] => hummel | + | f(&$a); /* Call-time pass-by-reference works, but is deprecated! */ |
- | ) | + | assert($a[0] == 1); |
+ | |||
+ | g($a); /* pass-by-reference */ | ||
+ | assert($a[0] == 2); | ||
+ | |||
+ | $b= h($a); /* return-by-reference seems to be default */ | ||
+ | assert($a[0] == 3); /* (pass-by-reference just to make it verifiable) */ | ||
+ | </code> | ||
+ | |||
+ | === Passing Arrays to Arrays == | ||
+ | |||
+ | <code php> | ||
+ | $a= array(0); | ||
+ | $b= array('d'=> $a, 'e'=> &$a); | ||
+ | |||
+ | $a[0]= 1; | ||
+ | assert($b['d'][0] == 0); | ||
+ | assert($b['e'][0] == 1); | ||
+ | </code> | ||
+ | |||
+ | === Passing Arrays to Objects == | ||
+ | |||
+ | <note important>In order to really make an object work with a reference of an arry, you have to //pass// it //by reference// **and** to //assign// it to the member variable //by reference//:</note> | ||
+ | |||
+ | <code php> | ||
+ | class C { | ||
+ | public function f(&$v) { $this->v= $v; $this->v[0]= 1; } | ||
+ | public function g(&$w) { $this->w= &$w; $this->w[0]= 2; } | ||
+ | } | ||
+ | $o= new C(); | ||
+ | $a= array(0); | ||
+ | |||
+ | $o->f($a); | ||
+ | assert($a[0] == 0); | ||
+ | |||
+ | $o->g($a); | ||
+ | assert($a[0] == 2); | ||
</code> | </code> | ||
Zeile 90: | Zeile 138: | ||
===== Logging == | ===== Logging == | ||
- | Use [[phpfn>function.error-log|error_log()]] for debug logging. | + | Use [[phpfn>function.error-log|error_log()]] for debug logging. |
+ | |||
+ | If the calling PHP script is run by the web server, the output usually goes to ''/var/log/httpd/error_log'' whereas if if the script is called from the command line, the output goes to ''stderr'' (tested). | ||
===== Command Line Usage == | ===== Command Line Usage == | ||
- | |||
- | 2004-02-13 14:45 | ||
- | |||
==== Prerequisites == | ==== Prerequisites == | ||
Zeile 103: | Zeile 150: | ||
* Optin -qC is necessary in shebang; therefore use it BY DEFAULT! (-q suppresses header; -C keeps working dir) | * Optin -qC is necessary in shebang; therefore use it BY DEFAULT! (-q suppresses header; -C keeps working dir) | ||
* set register_argc_argv = On in /etc/apache/php.ini | * set register_argc_argv = On in /etc/apache/php.ini | ||
- | |||
==== Command Line Arguments == | ==== Command Line Arguments == | ||
Zeile 109: | Zeile 155: | ||
<code php> | <code php> | ||
#!/usr/bin/php | #!/usr/bin/php | ||
+ | <?php | ||
echo "argument count: ".$_SERVER['argc']."\n"; | echo "argument count: ".$_SERVER['argc']."\n"; | ||
foreach ($_SERVER['argv'] as $name => $value) { | foreach ($_SERVER['argv'] as $name => $value) { | ||
echo "$name: $value\n"; | echo "$name: $value\n"; | ||
} | } | ||
+ | ?> | ||
</code> | </code> | ||
Zeile 128: | Zeile 176: | ||
==== Standard In- and Output == | ==== Standard In- and Output == | ||
+ | |||
+ | This is only useful for PHP command line scripts, not for webserver scripts. | ||
+ | |||
=== stdin == | === stdin == | ||
- | Read stdin line by line into the array lin: | + | Read stdin line by line: |
<code php> | <code php> | ||
- | char lin[MAX_LINE_LEN]; | + | while (fgets(STDIN)) {/* do some thing */} |
- | while (fgets(lin, MAX_LINE_LEN, stdin)) {/* do some thing */} | + | |
</code> | </code> | ||
- | === stdout, stderr ===== | + | === stdout == |
<code php> | <code php> | ||
- | printf(); echo; // writing to stdout | + | printf(); |
- | ; // writing to stderr | + | echo; |
- | error_log("Value $val"); // writing to System error log eg. /var/log/apache/error_log | + | |
</code> | </code> | ||
+ | |||
+ | === stderr == | ||
+ | |||
+ | <code php> | ||
+ | /* writing to stderr | ||
+ | Note that STDERR seems only to be available when script is called from command line */ | ||
+ | fputs(STDERR, "Some msg!\n"); | ||
+ | </code> | ||
+ | |||
+ | See also [[#logging]] | ||
+ | |||
+ | ===== Handling own libraries == | ||
+ | ==== Requirements == | ||
+ | |||
+ | - We want to intstall the lib somewhere under ''/usr/local/...'' to separate it from files of the Linux distribution | ||
+ | - We don't want to interfere with the C libraries in ''/usr/local/lib/'' | ||
+ | - The lib shall be easy to inlcude without the need for an absolute pathname. | ||
+ | - We don't want to tweak the ''include_path'' in ''php.ini'' | ||
+ | - Older code which uses the lib must not break when the API of the lib changes | ||
+ | |||
+ | ==== Solution == | ||
+ | |||
+ | Example with a self-written PHP library called ''mystuff'': | ||
+ | |||
+ | * Install the lib in ''/usr/local/lib/php/mystuff0''. This fulfills 1 and 2 | ||
+ | * Make a symlink from a dir which is specified in the PHP ''include_path'' to the real lib, eg: ''/usr/lib/php/mystuff0 -> /usr/local/lib/php/mystuff0''. To find out what the ''include_path'' is, use ''phpinfo()''. This fulfills requirement #3 and #4 | ||
+ | |||
+ | <code php phpinfo.php> | ||
+ | <?php phpinfo(); ?> | ||
+ | </code> | ||
+ | |||
+ | The number appendix specifies the API version of the lib. When the API of the lib changes, then the version number must be increased. This is the case when exported functions are removed, when their behavior changes or when the signature of the function changes. Note that adding more functions (or classes) to the lib do not break the API. | ||
+ | |||
+ | With every new API you have to install a new lib and keep the older ones: | ||
+ | |||
+ | /usr/lib/php/mystuff0 -> /usr/local/lib/php/mystuff0 | ||
+ | /usr/lib/php/mystuff1 -> /usr/local/lib/php/mystuff1 | ||
+ | /usr/lib/php/mystuff2 -> /usr/local/lib/php/mystuff2 | ||
+ | |||
+ | It should be ovious that you should avoid to change the API too often! | ||
+ | |||
+ | |||
+ | Note that in C you need not to specify a version number. This is resolved by the linker which seems to automatically write a reference to the newest lib into the binary at compile time. | ||
===== Draft: Pages and Actions == | ===== Draft: Pages and Actions == | ||
Zeile 179: | Zeile 271: | ||
===== To do == | ===== To do == | ||
+ | * [[http://articles.sitepoint.com/article/php5-standard-library|Standard PHP Library SPL]] | ||
+ | * Check [[http://www.developer.com/lang/php/10-pear-packages-for-every-php-developers-toolbox.html|Auth, HTML_QuickForm2, Text_CAPTCHA, Validate]] of Pear | ||
+ | * Check http://www.devshed.com/cp/bio/Alejandro-Gervasio/ | ||
* Check out: [[http://www.developer.com/open/article.php/10930_3782831|Sending Email with PHP]] and [[http://www.wdvl.com/Authoring/PHP/E-mail/Jason_Gilmore01102010.html|Practical PHP: Sending E-mail]] | * Check out: [[http://www.developer.com/open/article.php/10930_3782831|Sending Email with PHP]] and [[http://www.wdvl.com/Authoring/PHP/E-mail/Jason_Gilmore01102010.html|Practical PHP: Sending E-mail]] | ||
- | * Check out: http://www.php.net/autoload | ||
* Check active record pattern for database access | * Check active record pattern for database access |