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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。