Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Kohana3框架:MVC:从视图调用函数_Php_Foreach_Scope_Kohana 3 - Fatal编程技术网

Php Kohana3框架:MVC:从视图调用函数

Php Kohana3框架:MVC:从视图调用函数,php,foreach,scope,kohana-3,Php,Foreach,Scope,Kohana 3,控制器将$results传递给视图。 该观点有: <?php foreach ($results as $result): ?> <?php echo $result->time_start.' '.$result->time_finished;?> <?php endforeach;?> 另外,我想以hh:mm:ss格式显示每个$result的差异($result->time\u finished减去$result->time\u star

控制器将
$results
传递给视图。 该观点有:

<?php foreach ($results as $result): ?>
<?php echo $result->time_start.' '.$result->time_finished;?>
<?php endforeach;?>

另外,我想以
hh:mm:ss
格式显示每个$result的差异(
$result->time\u finished
减去
$result->time\u start
)。如果mm我会这样做: 我将编写一个静态函数来格式化日期(使用我自己的类或扩展Kohana
Date
class)。 在您看来,我会直接使用此功能:

<?php echo SomeClass::date_format($result->time_start, $result->time_finished) ?>

为什么会这样?因为函数用于格式化,所以它实际上不会返回不同的数据集。

下面是我要做的: 我将编写一个静态函数来格式化日期(使用我自己的类或扩展Kohana
Date
class)。 在您看来,我会直接使用此功能:

<?php echo SomeClass::date_format($result->time_start, $result->time_finished) ?>


为什么会这样?由于该函数用于格式化,因此它实际上不会返回不同的数据集。

如果您有一个函数需要在代码中多次使用,则1是有意义的。创建自己的助手或2。扩展Kohana的内置日期助手。助手用于此类操作,而不是模型,如果代码变得复杂,也不是视图

我不知道您是使用Kohana 3.2还是3.3,但它们之间的命名约定有点不同(因为实现了3.3 PSR-0,这意味着文件名和类名大写),但以下是帮助文档:

以及扩展现有课程:

如果您使用Kohana 3.2,我将提供答案:

解决方案1:扩展Kohana Date类是有意义的。在application/classes中创建一个名为date.php的文件。该文件应如下所示:

class Date extends Kohana_Date {
  public static function some_function_to_convert($date1, $date2) { } ;
}
<?php defined('SYSPATH') or die('No direct script access.');

class helper_date_conversion {
  public static function some_function_to_convert($date1, $date2) { } ;
}
在您看来,可以这样做:
Date::some_function_to_convert($date1,$date2)

解决方案2:在application/classes/helper目录中创建自己的助手。将其命名为date_conversion.php。然后,您的文件如下所示:

class Date extends Kohana_Date {
  public static function some_function_to_convert($date1, $date2) { } ;
}
<?php defined('SYSPATH') or die('No direct script access.');

class helper_date_conversion {
  public static function some_function_to_convert($date1, $date2) { } ;
}

如果您有一个函数需要在代码中多次使用,那么1是有意义的。创建自己的助手或2。扩展Kohana的内置日期助手。助手用于此类操作,而不是模型,如果代码变得复杂,也不是视图

我不知道您是使用Kohana 3.2还是3.3,但它们之间的命名约定有点不同(因为实现了3.3 PSR-0,这意味着文件名和类名大写),但以下是帮助文档:

以及扩展现有课程:

如果您使用Kohana 3.2,我将提供答案:

解决方案1:扩展Kohana Date类是有意义的。在application/classes中创建一个名为date.php的文件。该文件应如下所示:

class Date extends Kohana_Date {
  public static function some_function_to_convert($date1, $date2) { } ;
}
<?php defined('SYSPATH') or die('No direct script access.');

class helper_date_conversion {
  public static function some_function_to_convert($date1, $date2) { } ;
}
在您看来,可以这样做:
Date::some_function_to_convert($date1,$date2)

解决方案2:在application/classes/helper目录中创建自己的助手。将其命名为date_conversion.php。然后,您的文件如下所示:

class Date extends Kohana_Date {
  public static function some_function_to_convert($date1, $date2) { } ;
}
<?php defined('SYSPATH') or die('No direct script access.');

class helper_date_conversion {
  public static function some_function_to_convert($date1, $date2) { } ;
}