I have following php class
class User implements IUser
{
public function __construct($i_objParent = NULL)
{
}
}
When I login successfully I create object of User class.
There is another class named Student class as follows which extends User Class
class Student extends User
{
public function __construct($i_objParent = NULL)
{
parent::construct($i_objParent);
}
}
Now as I said eariler I already have object of User class
how can I construct Student object
from existing User Class
.
I think it may be possible by passing existing User class
object to constructor of child class here Student class
object?
Also, Is above approach OK?
As you cannot cast objects in php ( well there are really ugly hacks that work, but i would avoid them ).
Instead there are two ways:
User
instance in returns you new Student
instance with all the data transferedUser
instanceActually there is third way ( one that i would use ) : do not do this.
PHP is not the language for DCI development paradigm. IMHO this whole construction makes no sense. User
and Student
are not interchangeable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With