Php 摘录';名称';从init方法上的对象数组
我有以下代码:Php 摘录';名称';从init方法上的对象数组,php,Php,我有以下代码: add_action('manage_recipe_posts_custom_column', function($column, $post_id) { switch ($column) { case 'authors': $test = Recipe::init($post_id); var_dump($test); break; } }, 10, 3); 这给了我这个
add_action('manage_recipe_posts_custom_column', function($column, $post_id) {
switch ($column) {
case 'authors':
$test = Recipe::init($post_id);
var_dump($test);
break;
}
}, 10, 3);
这给了我这个对象数组的输出:如何仅将所有作者的姓名提取到回音中?首先,访问配方对象的authors属性。您需要一个可访问的方法,因为它是私有的。这将为您提供一个配置文件对象数组。循环该数组并使用可访问的方法获取name属性,因为它是私有的。然后你想用这些信息做什么就做什么 它应该是这样的:
$recipe = Recipe::init($post_id);
$authors = $recipe->getAuthors(); // This needs to be a method of the Recipe class.
foreach($authors as $author) {
echo $author->getName(); // This needs to be a method of the Profile class.
echo "\n";
}
相应地设置格式。可能会粘贴数组的简短可读文本。您必须通过该配方类实际提供的任何方法来执行此操作-因为该属性是私有的,您无法直接从外部访问它。这个类要么需要提供一个通用的getter方法,要么需要一个专门请求该特定属性内容的方法!你能投票支持我的职位吗?