我是OOP术语的新手,我正在尝试创建一个能产生一个命中计数器的类。
我尝试了下面的代码,但它只创建了一个内部值为1的counter.txt页面,我不知道为什么它不递增。
class LOGFILE {
public function READ($FileName) {
$handle = fopen($FileName, 'r');
$fread = file_get_contents($FileName);
return $fread;
fclose($handle);
}
public function WRITE($FileName, $FileData) {
$handle = fopen($FileName, 'w');
$FileData = $fread +1;
fwrite($handle, $FileData);
fclose($handle);
}
}
$logfile = new LOGFILE();
$logfile -> WRITE("counter.txt",$FileData);
echo $logfile -> READ("counter.txt");发布于 2012-12-11 15:42:20
原因是对于READ和WRITE方法,$fread都是局部变量。你需要让它成为你的类的private全局变量:
class LOGFILE {
private $fread;
public function READ($FileName) {
$this->fread = file_get_contents($FileName);
return $this->fread;
}
public function WRITE($FileName) {
$this->READ($FileName);
$handle = fopen($FileName, 'w');
$FileData = $this->fread +1;
fwrite($handle, $FileData);
fclose($handle);
}
}
$logfile = new LOGFILE();
$logfile -> WRITE("counter.txt");
echo $logfile -> READ("counter.txt");注意:我删除了fopen和fclose,因为file_get_contents不需要它们。在编写时,您可以使用file_put_contents。也删除了未使用的变量$FileData。在需要的时候创建变量、方法和类总是一个很好的实践。
还可以看看如何命名类、变量、方法等的最佳实践。Here's best guide,IMO.
发布于 2012-12-11 16:05:27
让我们开始检查更正后的代码,看看遗漏了什么:
<?php
class LOGFILE {
public function READ($FileName) {
$handle = fopen($FileName, 'r');
$fread = fgets($handle, 8192);
fclose($handle);
return $fread;
}
public function WRITE($FileName, $FileData) {
$counter = $this->READ($FileName);
$handle = fopen($FileName, 'w');
fwrite($handle, $FileData + $counter);
fclose($handle);
}
}
$logfile = new LOGFILE();
$FileData = 1;
$logfile -> WRITE("counter.txt",$FileData);
echo $logfile -> READ("counter.txt")."\n";
$logfile -> WRITE("counter.txt",$FileData);
echo $logfile -> READ("counter.txt")."\n";
?>fgets而不是file_get_contents (您可以选择使用file_get_contents,但我更愿意使用使用fopen)的其他函数
使用write权限打开文件的
'w'初始化$FileData = 1;的
不需要持有私有成员的
$freadreturn之后写语句(就像你在READ中做的那样)--在return之后写的语句将不会被执行!此解决方案已成功测试。
发布于 2012-12-11 15:45:39
OOP必须在需要的地方使用。你需要一个简单的东西,所以,不需要OOP。
<?php
function addValue($file='counter.txt', $amount=1) {
if( false == is_file($file) ) {
return false;
}
$initial = file_get_contents($file);
return @file_put_contents($initial+$amount);
}
addValue();
?>在一些复杂的东西上测试你的OOP知识,比如购物车或其他一些概念。
编辑 //因此,如果您需要一个看起来很复杂的简单示例,请转到这里:)
<?php
class log {
public $file = '';
private $amount = 0;
public function __construct( $file ) {
$this->file = $file;
$this->amount = 1;
}
public function makeAdd() {
$initial = file_get_contents($this->file);
return @file_put_contents($this->file, $initial + $this->amount);
}
function __call($f, $args) {
switch( $f ) {
case 'add':
if(isset($args[0]) && !empty($args[0])) {
$this->amount = (int)$args[0];
}
if( $this->amount == 0 ) {
throw new Exception('Not a valid amount.');
}
return $this->makeAdd();
break;
}
}
}
try {
// create log
$L = new log('count.txt');
// this will add 2
var_dump($L->add(2));
// this will also add 2
var_dump($L->add());
// until you rewrite the amount
var_dump($L->add(1));
// final result -> 5
} catch(Exception $e) {
die($e->getMessage());
}
?>祝好运!
https://stackoverflow.com/questions/13815660
复制相似问题