【问题标题】:String interpolation for expression in associative array关联数组中表达式的字符串插值
【发布时间】:2011-10-16 19:58:48
【问题描述】:

我有这样的定义:

define('LIBRARIES_URI', get_template_directory_uri() . '/libraries/');

现在,我有一个关联数组,例如:

array( 'name' => 'superfish-css', 'path' => LIBRARIES_URI .'superfish/css/superfish.css', 'type' => 'style' )

但是,路径不正确,这给了我一个错误,因为我猜有一个表达式。我尝试了各种东西,比如双引号之类的东西,但如果没有外部变量,我似乎无法正确处理。有没有办法正确插值?

完整示例:

define('LIBRARIES_PATH', TEMPLATEPATH . '/libraries/');


class Kosmos
{

  private static $inclusions = array(

    array( 'name' => 'style.css', 'path' => "get_bloginfo('stylesheet_url')", 'type' => 'style' ),
    array( 'name' => 'jquery', 'path' => '', 'type' => 'script' ),
    array( 'name' => 'jquery-ui-core', 'path' => '', 'type' => 'script' ),
    array( 'name' => 'jquery-ui-tabs', 'path' => '', 'type' => 'script' ),
    array( 'name' => 'superfish-css', 'path' => LIBRARIES_URI .'superfish/css/superfish.css', 'type' => 'style' ),
    array( 'name' => 'superfish-js', 'path' => "LIBRARIES_URI . 'superfish/js/superfish.js'", 'type' => 'script', 'dep' => array('jquery') ),
    array( 'name' => 'superfish-hover', 'path' => "LIBRARIES_URI . 'superfish/js/hoverIntent.js'", 'type' => 'script', 'dep' => array('jquery') ),
    array( 'name' => 'jquery-color', 'path' => "LIBRARIES_URI . 'horizontalMenu/jquery.color-RGBa-patch.js'", 'type' => 'script' ),

    /* admin */

    array( 'name' => 'admin-css', 'path' => "ADMIN_CSS_URI . 'admin.css'", 'type' => 'admin-style' ),
    array( 'name' => 'tabs-js', 'path' => "LIBRARIES_URI . 'tabs/tabs.js'", 'type' => 'admin-script' )

  );

  private static $admin_inclusions = array();

  public function inclusions() {
    return self::$inclusions;
  }

  public function admin_inclusions() {
    return self::$admin_inclusions;
  }  


}

【问题讨论】:

  • 您遇到的具体错误是什么?
  • 这里没有看到任何错误...
  • 嗯,您能更准确地说明您遇到的错误吗?如果我将function get_remplate_directory_uri() {return '/var/www';} 添加到顶部,这对我有用。
  • 请粘贴get_template_directory_uri...
  • 从问题推测,您正在尝试使用表达式声明类属性。那是不可能的。在构造函数中分配属性。

标签: php


【解决方案1】:

类属性声明可能包含表达式,只能包含文字/常量值。

将你的赋值移到构造函数中。

【讨论】:

  • 啊,有趣,所以这就是为什么会发生这种情况,我想我必须使用另一个变量。让我试试吧,我想这是唯一的方法。我可能不会避免它。
  • 是的,我最终在构造函数中完成了它。有趣的差异化。现在可以正常使用了。
猜你喜欢
  • 2022-01-20
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
相关资源
最近更新 更多