Cast derived class to base class in php

php cast class to subclass
php parent
php extend multiple classes
types of inheritance in php with example
php cast to class
php multiple inheritance
cast base class to derived class c++
type casting in php

I'm probably missing something obvious here as this is basic functionality in other OOP languages, but I'm struggling with PHP way of doing this. I understand that PHP is not "true" OOP language, but still...

What I'm after is casting an object instantiated as derived class to base class. Something along the lines of:

class Some{}

class SomeMock extends Some {}

function GetSomeMock(){
  $mock = new SomeMock();
  return (Some)$mock; //Syntax error here
}

I've found some questions with strange request of downcasting objects of base class to derived which is possible with some nasty tweaks, but this basic functionality does not have to be that difficult. What am I missing here?

Edit: It seems that it's always matter what I'm trying to achieve. No problem. GetSomeMock() is a factory method that would return a mock object stub (derived from base class, all properties prepopulated in constructor) with expected properties' values. I would then compare it to another object of base type that is restored from database:

$this->assertEquals($mockObject, $realObject);

This fails instantly as $mockObject and $realObject are of different types. I can imagine there are many workarounds I can implement to achieve the same, but I'd like to keep things as simple as possible.

Ok, the short answer seems to be: This is not possible. PHP knows better than me what type I need.

runkit_class_adopt - Manual, This rule applies to classes that inherit other classes and interfaces. Example #1 Inheritance Example. <?php class Foo { When you cast an instance of the derived type to the base class, then, essentially you are moving the base-class dictionary to the top, you hide the derived class dictionary, and so, the compiler locates the method bound to the method-name 'Print to execute in the base class dictionary.

In PHP you cannot cast to a specific class, I cannot even see any need for that. You may cast to some native type - string, int, array, object. But not a specific class.

If you need to use some functionality of a base class, you can do it via parent keyword.

class Some {
  public function execute(){
    echo "I am some" ;
  }
}

class SomeMock extends Some {
  public function execute(){ //Override the function
    parent::execute() ; //Here you can execute parent's functionality and add some more
  }
}

Edit:

instanceof operator may come handy. When comparing objects. For instance:

$object = new SomeMock() ;
$status = ($object instanceof Some && $object instanceof SomeMock); //will return true here ;

Child classes inherit non-private properties and methods.

Let's say, you have your function:

function assertEquals($mockObject, $realObject){
  if ($mockObject instanceof Some && $realObject instanceof Some){
    //Both objects have the same base class - Some
    //That means they must have inherited the functionality and properties of Some class.
  }
}

Object Inheritance - Manual, It is always allowed for public inheritance, without an explicit type cast. This is a result of the is-a relationship between the base and derived classes. Here is the  Object Inheritance. Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class.

You can do it with an custom function:

private function castParameter(BaseClass $parameters) : DerivedClass {
    return $parameters;
}

C++ Tutorial: Upcasting and Downcasting - 2020, Casting From Base to Derived Class. Jul 1st, 2009 | Comments. Today a friend of mine asked me how to cast from List<> to a custom MyCollection class, where  Upcasting is converting a derived-class reference or pointer to a base-class. In other words, upcasting allows us to treat a derived type as though it were its base type. It is always allowed for public inheritance, without an explicit type cast. This is a result of the is-a relationship between the base and derived classes.

I did come across use cases when I needed to cast a derived class to its base class. I did the following.

function castToParentClass($derivedClassInstance) {
    $parentClassName = get_parent_class($derivedClassInstance);

    $parentClassInstance = new $parentClassName();
    foreach ($parentClassInstance as $key => $value) {
        $parentClassInstance->{$key} = $derivedClassInstance->{$key};
    }

    return $parentClassInstance;
}

Casting From Base to Derived Class, <?php. /**. * Cast an object into a different class. *. * Currently this only supports casting DOWN the inheritance chain,. * that is, an object may only be cast into a  Any class type (whether declared with class-key class or struct) may be declared as derived from one or more base classes which, in turn, may be derived from their own base classes, forming an inheritance hierarchy. The list of base classes is provided in the base-clause of the class declaration syntax.

PHP function to cast an object from one class to another. · GitHub, Converting an object to another class of same inheritance What I want to do is convert an object of type pending_order to order, and use the  You can’t use static_cast in this situation because the compiler doesn’t know the offset of B relative to A at compile time. The offset must be calculated at run-time based on the exact type of the most derived object.

Converting an object to another class of same inheritance, You will get in such a situation when you convert between base class and derived class variables ! The effect of assigning a derived class reference to a base  Inheritance (Derived and Base Class) In C#, it is possible to inherit fields and methods from one class to another. We group the "inheritance concept" into two categories: Derived Class (child) - the class that inherits from another class; Base Class (parent) - the class being inherited from; To inherit from a class, use the : symbol.

"Converting" between base class and derived class types, My aim is to cast the Derived class object to a Base class object. Pls look at the code class Base { public int i; Base() An assignment of derived class object to a base class reference in C# inheritance is known as up-casting. The up-casting is implicit and any explicit typecast is not required. For example, in the below program in the main() method, assignment of the derived class “Circle” object to the base class “Shape” reference is up-casting.

Comments
  • What are you trying to achieve? This is needed in some languages because of static type checks, but PHP uses dynamic typing.
  • I understand that PHP is not "true" OOP language This is not accurate, especially since version 5.3.
  • You are answering some other question. I've edited mine clarifying why I need that.
  • Yes, I understand how to achieve the same goal by implementing additional checks and methods. But I thought that direct comparison would be the easiest way (if proved possible). And after all, I have no control over assertEquals method - it comes from PHPUnit library.