Php 在方法内定义foreach以避免重复
因此,我的类中有两个方法使用相同的Php 在方法内定义foreach以避免重复,php,Php,因此,我的类中有两个方法使用相同的foreach,它们在两个方法中重复。。在foreach自己的方法中定义foreach有没有最好的方法?因此,我希望在方法内部将foreach调用为$this->loop,然后在名为public function loop()的方法内部调用foreach 我们将不胜感激 因此,我的类中有以下两种方法: public function check_missing_blobs(): void { $profiles = Profile::ge
foreach
,它们在两个方法中重复。。在foreach自己的方法中定义foreach有没有最好的方法?因此,我希望在方法内部将foreach调用为$this->loop
,然后在名为public function loop()
的方法内部调用foreach
我们将不胜感激
因此,我的类中有以下两种方法:
public function check_missing_blobs(): void
{
$profiles = Profile::get([
'meta_query' => [
'relation' => 'OR',
[
'key' => 'azure_picture',
'value' => '',
'compare' => '='
],
[
'key' => 'azure_picture_big',
'value' => '',
'compare' => '='
],
]
]);
if (count($profiles) === 0) {
return;
}
foreach ($profiles as $profile) {
$profile->load([
'azure_picture' => Azure::init()->set_blob_sas_url
($profile->get_employee_id()),
'azure_picture_big' => Azure::init()->set_blob_sas_url
($profile->get_employee_id(), 'Big/'),
'azure_picture_expiration' =>
$profile->set_azure_picture_expiration
(strtotime('+7 days')),
'azure_picture_big_expiration' =>
$profile->set_azure_picture_big_expiration
(strtotime('+7 days')),
]);
$profile->save();
}
}
以及:
public function check\u blob\u expiration(字符串$days='+2天'):bool
{
$key='check_blob_expiration';
$settings=$this->get\u settings($key);
如果($settings->start-finished){
$settings->start=time();
}
$profiles=Profile::get([
“post_状态”=>[
“发布”、“草稿”、“待定”、“私有”
],
“每页帖子数”=>250,
“元查询”=>[
'关系'=>'或',
[
“密钥”=>“azure图片过期”,
“值”=>strottime($days),
“比较”=>“您可以设想使用
简而言之,有一个特性允许您:在不同类层次结构中的几个独立类中自由重用方法集
有助于更好理解该概念的示例:
定义一个特征,称它为,例如,Iterate
,这里它有一个函数循环(数组$arr)
在要使用函数循环
的类中,使用:使用迭代
现在,如您所见,我们可以调用函数:$this->loop()
在类Test
你可以设想使用
简而言之,有一个特性允许您:在不同类层次结构中的几个独立类中自由重用方法集
有助于更好理解该概念的示例:
定义一个特征,称它为,例如,Iterate
,这里它有一个函数循环(数组$arr)
在要使用函数循环
的类中,使用:使用迭代
现在,如您所见,我们可以调用函数:$this->loop()
在类Test
哦,我忘了这一切!让我测试一下,我会很快触及基础!哦,我忘了这一切!让我测试一下,我会很快触及基础!
public function check_blob_expiration(string $days = '+2 days'): bool
{
$key = 'check_blob_expiration';
$settings = $this->get_settings($key);
if ($settings->started <= $settings->finished) {
$settings->started = time();
}
$profiles = Profile::get([
'post_status' => [
'publish', 'draft', 'pending', 'private'
],
'posts_per_page' => 250,
'meta_query' => [
'relation' => 'OR',
[
'key' => 'azure_picture_expiration',
'value' => strtotime($days),
'compare' => '<=',
],
[
'key' => 'azure_picture_big_expiration',
'value' => strtotime($days),
'compare' => '<=',
],
],
'paged' => $settings->page,
]);
if (count($profiles) === 0) {
$settings->page = 0;
$settings->finished = time();
echo '<pre>No azure blobs expiring within 2 days.</pre>';
return $this->set_settings($key, $settings);
}
foreach ($profiles as $profile) {
$profile->load([
'azure_picture' => Azure::init()->set_blob_sas_url
($profile->get_employee_id()),
'azure_picture_big' => Azure::init()->set_blob_sas_url
($profile->get_employee_id(), 'Big/'),
'azure_picture_expiration' =>
$profile->set_azure_picture_expiration
(strtotime('+7 days')),
'azure_picture_big_expiration' =>
$profile->set_azure_picture_big_expiration
(strtotime('+7 days')),
]);
$profile->save();
}
if (!empty($profiles)) {
$settings->page++;
} else {
$settings->page = 0;
$settings->finished = time();
}
return $this->set_settings($key, $settings);
}
<?php
trait Iterate
{
public function loop(array $arr)
{
foreach ($arr as $key => $value) {
echo 'key : ' . $key;
echo '<br />';
echo 'value : ' . $value;
echo '<br />';
}
}
}
class Test
{
use Iterate;
public function __construct($arr)
{
$this->loop($arr);
}
}
$arr = [1, 2, 3];
$test = new Test($arr);
key : 0
value : 1
key : 1
value : 2
key : 2
value : 3