Php从对象实例

时间:2018-04-11 13:28:50

标签: php symfony

我试图访问我的SheriffUser实例的属性,将它们存储在单独的变量中并对其进行操作($ email,$ password,$ fullname ...) 我试着做一个get_object_vars但它什么也没有返回。

实例化不起作用,因为缺少属性。

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());
    }
}

以下是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
}

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 } 

1 个答案:

答案 0 :(得分:1)

试试这个例子:

$email = $user->getEmail();

通常在你的实体中你有getter和setter因为属性是私有的,在这种情况下我使用了getter getEmail