[英]Class Variables Not Staying Set (PHP)
Alright, I'm not sure exactly 100% what's going on here, but I think it has to do with the fact that I'm trying to include a class using php's "include($file)" function. 好吧,我不确定100%这里发生了什么,但我认为这与我试图使用php的“include($ file)”函数包含一个类的事实有关。
the function import looks like this: 函数import看起来像这样:
<?php
function import($file) {
global $imported; $imported = true;
$home_dir = "C:/xampp/htdocs/includes/";
if (file_exists($home_dir.$file.".php")) {
include_once($home_dir.$file.".php");
}
$imported = false;
}
?>
All I do is call the following php in my index.php file: 我所做的就是在index.php文件中调用以下php:
<?php
import("php.buffer");
$out = new StringBuffer;
$out->write("test?");
echo "'".($out->get())."' <- Buffer String Should Be Here";
?>
The php.buffer.php file looks like this: php.buffer.php文件如下所示:
<?php
class StringBuffer {
public $buffer = "";
public function set($string) {
if (!isset($buffer)) { $buffer = ""; }
$buffer = $string;
}
public function get() {
if (!isset($buffer)) { $buffer = ""; }
return $buffer;
}
public function write($string) {
if (!isset($buffer)) { $buffer = ""; }
$buffer = $buffer.chr(strlen($string)).$string;
}
public function read() {
if (!isset($buffer)) { $buffer = ""; }
$return = "";
$str_len = substr($buffer,0,1); $buffer = substr($buffer,1,strlen($buffer)-1);
$return = substr($buffer,0,$str_len); $buffer = substr($buffer,$str_len,strlen($buffer)-$str_len);
return $return;
}
public function clear() {
$buffer = "";
}
public function flushall() {
echo $buffer;
$this->clear();
}
public function close() {
return new NoMethods();
}
}
?>
I don't get any errors when I create the new StringBuffer class, so I know that it is indeed including my files. 我创建新的StringBuffer类时没有出现任何错误,因此我知道它确实包含了我的文件。
What's going on here is that inside your class methods, instead of accessing properties ( $this->buffer
) you are getting and setting local variables ( $buffer
) so the changes don't "stick". 这里发生的是在你的类方法中,而不是访问属性( $this->buffer
),你得到并设置局部变量( $buffer
),因此更改不会“粘住”。
That code could use some cleaning up too. 该代码也可以使用一些清理工具。 There is lots of redundant stuff in there, for example: 那里有很多冗余的东西,例如:
public function set($string) {
// isset will never return false, so this if will never execute
// even if it did, what's the purpose of setting the buffer when you
// are going to overwrite it one line of code later?
if (!isset($this->buffer)) { $this->buffer = ""; }
$this->buffer = $string;
}
I think that your class should be 我认为你的课应该是
class StringBuffer {
private $buffer = "";
public function get() {
if (!isset($this->$buffer)) { $this->$buffer = ""; }
return $this->$buffer;
}
public function write($string) {
if (!isset($this->$buffer)) { $this->$buffer = ""; }
$this->$buffer = $this->$buffer.chr(strlen($string)).$string;
}
}
in this way you are setting a class variable, with your code you are just setting variables inside methods 这样你就可以设置一个类变量了,你的代码就是在方法中设置变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.