Jump to content

pepito

New Members
  • Posts

    4
  • Joined

  • Last visited

pepito's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. @requinix.... you're absolutely RIGHT and so I was ... not really. Here was my confusion... from a .NET perspective, yes, during runtime, "this" in the Parent becomes the child when you create an instance of the child. Your example is perfect. As a matter of fact, you don't even need :base(). As long as there is a constructor in the parent class, it will step in. What threw me off was this. At "design" time, in .NET, "this" in the parent class, really responds to methods and variables that are in the class scope. In other words, you would get a compilation error. So, if I have a method in my Child, or properties, I wouldn't be able to reach them from my parent class while writing code. So, when I saw that Configure was taking $this as the first parameter, AND assigning values to some "properties", that's when I got confused and hit by the bus. I was like..."how can this method pass values to this class (Object = $this) when it doesn't have any of these members???? Dynamic assigning??? And that's what it was. Pretty much if I create a method in my class Config and use reflection, I could've achieved the same in .NET. But of course, php already comes equipped with its own "reflection". Now it all makes sense. Once again, THANK YOU TONS for your attention to this doubts.
  2. @requinix.... Once again, thank you so much for your lengthy answers. NOW i got it... the static part. Perfect. One thing you got me confused about... let me quote. In this example I am presenting to you, $this inside of the constructor of the class Object, refers to Object, not to User. That's where I got confused. In .net in order to have an instance of the child in the parent you have to pass it in the constructor... public class Parent { private Child _child; public Parent(Child child) { this._child = child; // see? This here means Parent. Not Child. } } public class Child { public Child():base(this) { // See? this here means Child. That's how you pass a ref to Parent in .net } } So, when you say that $this in the Object constructor refers to User, that part confuses me. Do you see my point?
  3. @requinix Thank you so much for your answer. Definitely helped me clear up some of my doubts. Now, in plain English, let's see if I got this. User creates an instance of itself by using new static... (since the method getIdentity($id) was invoked in User? or new static was invoked?). By the way, I forgot to mention that getIdentity($id) actually belongs to an interface and according to the documentation returns an IdentityInterface? Confusing... Moving on. if I say.... $user = User::getIdentity($id)... it returns an instance of User... or IdentityInterface? Assuming it's User (for what I understood from your response....) Since User doesn't have a constructor, but it's parent class does, it executes the code inside Object's constructor. This constructor receives an array of key-values that dynamically are added to Object. Then User, since it has public properties that "matches" the same ones added to Object, it sort of "channels" those values up to User. Am I correct? Now, a couple of questions that pop up while i was writing the paragraph above... the reason they use new static and not new User is because findIdentity($id) is a static method that belongs to an interface? if I wanted to use new User I would've need to have a static factory method that returns an instance of User. So, basically new static is a "static factory method" for the class where new static was invoked on. Correct? Here are the three classes i am talking about and the interface. https://github.com/yiisoft/yii2/blob/master/framework/web/IdentityInterface.php https://github.com/yiisoft/yii2/blob/master/framework/base/Object.php http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2-revised-12-20-2013/#hh11 (User class)
  4. I come from a .NET world. Now entering these frigid php waters. I found an example that got me a little confused. Of course, I am trying to apply OOP fundamentals to this php code but it doesn't make sense. This is the class i am talking about. <?php namespace app\models; class User extends \yii\base\Object implements \yii\web\IdentityInterface { public $id; public $username; public $password; public $authKey; private static $users = [ '100' => [ 'id' => '100', 'username' => 'admin', 'password' => 'admin', 'authKey' => 'test100key', ], '101' => [ 'id' => '101', 'username' => 'demo', 'password' => 'demo', 'authKey' => 'test101key', ], ]; public static function findIdentity($id) { return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; } public static function findByUsername($username) { foreach (self::$users as $user) { if (strcasecmp($user['username'], $username) === 0) { return new static($user); } } return null; } public function getId() { return $this->id; } public function getAuthKey() { return $this->authKey; } public function validateAuthKey($authKey) { return $this->authKey === $authKey; } public function validatePassword($password) { return $this->password === $password; } } Alright, it's obvious to me that in the method findByIdentity($id) all it's doing is creating a static new instance of User. This is the first thing that caught me off guard. In .net you cannot create an instance of a static class. Now, moving on. in that line return isset(self::$users[$id])? new static(self::$users[$id]) : null; the second thing that intrigues me is the following. Since all you have in that array is a key/value collection.... private static $users = [ '100' => [ 'id' => '100', 'username' => 'admin', 'password' => 'admin', 'authKey' => 'test100key', ], '101' => [ 'id' => '101', 'username' => 'demo', 'password' => 'demo', 'authKey' => 'test101key', ], ]; how does php determine that it has to create an User object? Reflection? Which leads me to the next question.... looking at the class that it inherits from, Object, in the constructor, there's in one parameter which is an array (one of the elements of the array above). public function __construct($config = []) { if (!empty($config)) { Yii::configure($this, $config); } $this->init(); } BUT, this class in its constructor, is calling Yii::configure($this, $config) and in this method, the way I see it, Yii is adding to $this (the Object instance I am assuming, not the User one) the parameters that belong to User. public static function configure($object, $properties) { foreach ($properties as $name => $value) { $object->$name = $value; } return $object; } Seems to me like it's adding parameters dynamically to Object which will be accessed by User via the matching parameters. Makes sense? From my .net standpoint, $this in Object refers to Object instance itself, not to the User instance inheriting from it (like my friend says). I told him that's a violation of basic OOP principles and it's simply impossible. Anyone that could make me understand about this? Thank you.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.