小编Ale*_*lex的帖子

PHP:empty不适用于getter方法

我有一个像"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)

php getter class

13
推荐指数
1
解决办法
3444
查看次数

CSS中字体大小的最佳单位

每个的优点和缺点是什么?em,px,%pt

我目前的选择是百分比,唯一的原因是因为我可以通过修改根元素(正文)上的字体大小来全局更改所有元素的字体大小

css font-size units-of-measurement

13
推荐指数
2
解决办法
1万
查看次数

shmop PHP扩展有什么作用?

http://www.php.net/manual/en/intro.shmop.php

Shmop是一组易于使用的函数,允许PHP读取,写入,创建和删除Unix共享内存段.

我不明白,这个扩展目的究竟是什么?它是干什么用的?

php shared-memory

13
推荐指数
2
解决办法
9086
查看次数

使用__get()(魔术)来模拟readonly属性和延迟加载

我正在使用__get()使我的一些属性"动态"(仅在请求时初始化它们).这些"假"属性存储在私有数组属性中,我在__get中检查.

无论如何,你认为为每个属性创建方法更好,而不是在switch语句中进行吗?


编辑:速度测试

我只关心性能,@戈登提到的其他东西对我来说并不重要:

  • 不需要增加的复杂性 - 它并没有真正增加我的应用复杂性
  • 脆弱的非显而易见的API - 我特别希望我的API"孤立"; 文档应告诉其他人如何使用它:P

所以这里是我做的测试,这让我觉得表现不佳是不合理的:

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)

php oop performance

13
推荐指数
1
解决办法
701
查看次数

Javascript/Jquery - 从字符串中获取数字

字符串看起来像这样

"blabla blabla-5 amount-10 blabla direction-left"
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得之后的数字"amount-",以及之后的文本"direction-"

javascript string jquery

12
推荐指数
3
解决办法
4万
查看次数

PHP - 在AJAX请求中退出或死亡()?

这里的最佳做法是什么?使用die()还是exit()?这两者有什么区别?

if($_GET['do_thing']):
  echo 'bla bla';
  exit(); // or die(), or something else?
endif;
Run Code Online (Sandbox Code Playgroud)

php ajax json

12
推荐指数
1
解决办法
9811
查看次数

PHP - 空数组

(array)$someemptyvariablethatisnotarray返回array([0] =>)而不是array()

我怎么能这样做,所以当我在foreach()中使用它时,我得到一个没有迭代的空数组?

php arrays variables

12
推荐指数
2
解决办法
4万
查看次数

基于变量的条件CSS规则少

变量可以采用百分比或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)

css less

12
推荐指数
2
解决办法
2万
查看次数

如何在PHP中实现__isset()魔术方法?

我正在尝试使用类似函数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)

php class isset magic-methods

11
推荐指数
1
解决办法
7266
查看次数

使用include/require_once将内容分配给变量

是否可以这样做

$var = require_once('lol.php');
Run Code Online (Sandbox Code Playgroud)

所以任何HTML输出lol.php都会进入内部$var

我知道输出缓冲,但有一些特殊的内置函数已经做到了吗?

html php variables output-buffering

11
推荐指数
2
解决办法
8741
查看次数