我有一个像"getter"这样的方法
function getStuff($stuff){
return 'something';
}
Run Code Online (Sandbox Code Playgroud)
如果我检查它empty($this->stuff),我总是得到FALSE,但我知道$this->stuff返回数据,因为它适用于回声.
如果我检查它,!isset($this->stuff)我得到正确的值,条件永远不会执行...
这是测试代码:
class FooBase{
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter)) return $this->$getter();
throw new Exception("Property {$getter} is not defined.");
}
}
class Foo extends FooBase{
private $my_stuff;
public function getStuff(){
if(!$this->my_stuff) $this->my_stuff = 'whatever';
return $this->my_stuff;
}
}
$foo = new Foo();
echo $foo->stuff;
if(empty($foo->stuff)) echo 'but its not empty:(';
if($foo->stuff) echo 'see?';
Run Code Online (Sandbox Code Playgroud) 每个的优点和缺点是什么?em,px,%和pt?
我目前的选择是百分比,唯一的原因是因为我可以通过修改根元素(正文)上的字体大小来全局更改所有元素的字体大小
http://www.php.net/manual/en/intro.shmop.php
Shmop是一组易于使用的函数,允许PHP读取,写入,创建和删除Unix共享内存段.
我不明白,这个扩展目的究竟是什么?它是干什么用的?
我正在使用__get()使我的一些属性"动态"(仅在请求时初始化它们).这些"假"属性存储在私有数组属性中,我在__get中检查.
无论如何,你认为为每个属性创建方法更好,而不是在switch语句中进行吗?
我只关心性能,@戈登提到的其他东西对我来说并不重要:
所以这里是我做的测试,这让我觉得表现不佳是不合理的:
50.000次调用的结果(在PHP 5.3.9上):

(t1 =魔术与开关,t2 = getter,t3 =魔术与进一步的getter调用)
不确定t3上"Cum"的含义是什么.它不能累积时间,因为t2应该有2K然后......
代码:
class B{}
class A{
protected
$props = array(
'test_obj' => false,
);
// magic
function __get($name){
if(isset($this->props[$name])){
switch($name){
case 'test_obj':
if(!($this->props[$name] instanceof B))
$this->props[$name] = new B;
break;
}
return $this->props[$name];
}
trigger_error('property doesnt exist');
}
// standard getter
public function getTestObj(){
if(!($this->props['test_obj'] instanceof B))
$this->props['test_obj'] = new B;
return $this->props['test_obj'];
}
}
class AA extends A{ …Run Code Online (Sandbox Code Playgroud) 字符串看起来像这样
"blabla blabla-5 amount-10 blabla direction-left"
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得之后的数字"amount-",以及之后的文本"direction-"?
这里的最佳做法是什么?使用die()还是exit()?这两者有什么区别?
if($_GET['do_thing']):
echo 'bla bla';
exit(); // or die(), or something else?
endif;
Run Code Online (Sandbox Code Playgroud) (array)$someemptyvariablethatisnotarray返回array([0] =>)而不是array()
我怎么能这样做,所以当我在foreach()中使用它时,我得到一个没有迭代的空数组?
变量可以采用百分比或px值,例如:
@some-var: 50px; 要么 @some-var: 46%;
如果值以像素为单位,如何定义一组特定的CSS规则,如果值为百分比,如何定义一组不同的规则?
有没有类似的东西
if(isValueInPixels(@some-var)){
// css rules here
}else{
// other rules here
}
Run Code Online (Sandbox Code Playgroud)
?
我正在尝试使用类似函数empty()并isset()使用方法返回的数据.
到目前为止我所拥有的:
abstract class FooBase{
public function __isset($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return isset($this->$getter()); // not working :(
// Fatal error: Can't use method return value in write context
}
public function __get($name){
$getter = 'get'.ucfirst($name);
if(method_exists($this, $getter))
return $this->$getter();
}
public function __set($name, $value){
$setter = 'set'.ucfirst($name);
if(method_exists($this, $setter))
return $this->$setter($value);
}
public function __call($name, $arguments){
$caller = 'call'.ucfirst($name);
if(method_exists($this, $caller)) return $this->$caller($arguments);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
class Foo extends FooBase{
private $my_stuff;
public function …Run Code Online (Sandbox Code Playgroud) 是否可以这样做
$var = require_once('lol.php');
Run Code Online (Sandbox Code Playgroud)
所以任何HTML输出lol.php都会进入内部$var?
我知道输出缓冲,但有一些特殊的内置函数已经做到了吗?