doukefu1361 2018-04-11 13:28
浏览 57
已采纳

Php从对象实例中提取数据[重复]

This question already has an answer here:

I'm trying to access the properties of my SheriffUser instance to store them in separate variables and manipulate them ($email, $password, $fullname...) I tried to do a get_object_vars but it returns nothing.

An instantiation doesn't work either because there are missing properties.

namespace Aecf\DemoBundle\AuthTest;

use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Aecf\SecurityBundle\Security\Core\User;

class AuthTestPrint
{
    public function printSomething(AuthenticationEvent $event){
        dump('Authentication detected');
        $token = $event->getAuthenticationToken();
        $user=$token->getUser();
        dump($user);
        var_dump($user);
        //$user = new User\SheriffUser($token->getUser());
    }
}

Here is the result of dump(user);

AuthTestPrint.php on line 17:
SheriffUser {#443 ▼
  -email: "user"
  -actions: array:1 [▶]
  -password: "user"
  -fullName: "user"
  -accessToken: "iAnytnwIwEJSUzI1NiJ9.UBhnIBsOlgFtZSI6IlVzZXIiLCJ1c2VybmFtZSI6InVzZXIiLCJleHAiOjMzMDQyMTY0MDcxLCJpYXQiOjE0ODUyNTUyNzF9.LQU1NV8FMZGH9b3r7Rn4BrPK5Qany6ym_EyA9MMteI ▶"
  -shops: ArrayCollection {#444 ▶}
  -locale: null
  -refreshToken: null
  -expiresAt: null
}

The result of var_dump($user);

object(Aecf\SecurityBundle\Security\Core\User\SheriffUser)#443 (9) { ["email":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> string(4) "user" ["actions":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> array(1) { [0]=> array(2) { ["type"]=> string(7) "generic" ["name"]=> string(9) "ROLE_USER" } } ["password":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> string(4) "user" ["fullName":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> string(4) "user" ["accessToken":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> string(801) "iAnytnwIwEJSUzI1NiJ9.UBhnIBsOlgFtZSI6IlVzZXIiLCJ1c2VybmFtZSI6nVzZXIiLCJleHAiOjMzMDQyMTY0MDcxLCJpYXQiOjEODUyNTUyNzF9.LQU1NV8FMZGH9b3r7Rn4BrPK5Qany6ym_EyA9MMteImlDrjAs45vIvr0BLKsU4N7isvGGRDc_MXphWUm6p61LMVTcFOGmlERd1B2BEmWkkDVTKEy5jBu7vOVHqK5LYLseAB4K3Qxk17cDe80uO1vutbeb0syWykuspHuWunr_tTqoot70jYCEmbhv_4jsxnxowpsoL13iIkkqGdtdLjxTQ5RQE5sjhXnguiHDeC4Fhm1-gsptQmCp00yzdq9GcPFurddtP_MWNjX4NwqhuZjiIVeQqG154ocbF_Z49a6aj2tLsNY_vpinaCcwvNL5Yshg1n964JrQZM4U0ysrWE1oPI887rhHBilwrtk1l1Z5czUH8zVLMzeAzSMr0MvidjM01nKidbbVJBXmaACoyqcftFNAG7_CZijW2oYlZe7UiJ71cpvrxzOr1Tlpgs4YmvxRC4bIwijdI_QJXwDmL4Q-JfRfAu4g9JigEoCHLV-oU56D_e2Btd3X4uL7EII-F--0LKcuFb1_eK8GTvcECz7rRAPhpv0Hi4kxJfIQtKFvOnJ3W0JA0X5d_-x2Jn__TdArZIVYWChnWKFw2tv7UuQ9ymGoUiq7qH0OSPmF7DMQtudFE7bszyaK1X_wVhHSFej82HVqL_SVC4epEle5_53xrJCIuTIgl0sE" ["shops":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> object(Doctrine\Common\Collections\ArrayCollection)#444 (1) { ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(0) { } } ["locale":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> NULL ["refreshToken":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> NULL ["expiresAt":"Aecf\SecurityBundle\Security\Core\User\SheriffUser":private]=> NULL } 
</div>
  • 写回答

1条回答 默认 最新

  • douke3442 2018-04-11 13:32
    关注

    try this for example:

    $email = $user->getEmail();
    

    Usually in your entity you have getter and setter because property are private, in this case I have use a getter getEmail.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器