在课堂上使用__DIR__ [英] usage of __DIR__ in a class
本文介绍了在课堂上使用__DIR__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个非常简单的PHP应用程序,可以稍微修改文件的路径。
这是我的代码:
<?php
class abc {
private $ path = __DIR__。 DIRECTORY_SEPARATOR。 'moshe'。 DIRECTORY_SEPARATOR;
function doPath(){
echo $ this-> path;
}
}
$ a = new abc();
$ a-> doPath();
我收到错误:
PHP解析错误:语法错误,意外的'。',expecting','或';'在/mnt/storage/home/ufk/1.php第4行
pre>
解析错误:语法错误,意外的'。',expecting','或';'在/mnt/storage/home/ufk/1.php第4行
由于某些原因,我无法使用。添加连接 __ DIR __ 到另一个字符串。我失踪了什么?
使用PHP 5.5.13。
解决方案字段dinamycally
private $ a = 5 + 4; //不工作,必须被评估
private $ a = 9; //工作,因为静态值
您的解决方案:
class abs {
private $ path;
public function __construct(){
$ this-> path = __DIR__。 DIRECTORY_SEPARATOR。 'moshe'。 DIRECTORY_SEPARATOR;
}
}
I'm writing a very simple PHP application that returns the path of the file with a slight modification.
this is my code:
<?php class abc { private $path = __DIR__ . DIRECTORY_SEPARATOR. 'moshe' . DIRECTORY_SEPARATOR; function doPath() { echo $this->path; } } $a = new abc(); $a->doPath();
I get the error:
PHP Parse error: syntax error, unexpected '.', expecting ',' or ';' in /mnt/storage/home/ufk/1.php on line 4 Parse error: syntax error, unexpected '.', expecting ',' or ';' in /mnt/storage/home/ufk/1.php on line 4
for some reason I cannot add connect __DIR__ using '.' to another string. what am I missing?
using PHP 5.5.13.
解决方案You cannot define class fields dinamycally
private $a = 5 + 4; // wont work, has to be evaluated private $a = 9; // works,because static value
Your solution:
class abs{ private $path; public function __construct(){ $this->path = __DIR__ . DIRECTORY_SEPARATOR. 'moshe' . DIRECTORY_SEPARATOR; } }
这篇关于在课堂上使用__DIR__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文