Can I instantiate a child when when the parent is instantiated?
I am trying to use inheritance to override a parent function. But I can't just create a new instance of the child because the variable for the parent constructor are not present at the time.
Basically, I am trying to extend a Wordpress plugin and I do not want to change the core plugin files. When the plugin calls for the parent object to be created, I want the the child to instantiate when that happens to override one of the parent functions.
Example:
return new WC_Subscription( $subscription_id );
My class extends WC_Subscription, and My class does one thing and that is override one function through inheritance.
Related
I have declared a PHP magic function __destruct in child class, when an object from this child class is called it also calls the __destruct from the parent class in addition to the __destruct from the child class.
How do I make sure that only the __destruct in the child class is called. I do not want to use access modifiers too because I might be creating another child class.
I have a Wordpress parent theme that my child theme is extending a lot of functionality from. My issue though is that when I want to use or extend a class from the Parent them in my child theme folder structure, I get errors of "class is not defined" or "class not found"
What is the correct way to use classes from the parent theme?
Here is an example of one of the classes:
class MyNewClass extends ReadAndDigestWidget {}
And that class will call more classes inside it that are from the parent theme.
Any help in understanding Wordpress and php more deeply is greatly appreciated!
PHP is not an OOP language by design so the results can be trivial at times. Here is a basic example of how inheritance works in PHP
class ChildTheme extends ParentTheme {
function parentDropDownMenu(){
parent::dropDownMenu();
//do additionall stuff
}
}
theme = new ParentTheme();
$theme->dropDownMenu(); // will call the parent function directly
$theme->parentDropDownMenu(); // will call the function override
Typically I wouldn't even override my parent's class methods in PHP but merely extend the class if I don't have complete control over the parent class. What if an upgrade takes place in wordpress and you override a must needed upgrade?
For my latest website I'm trying to use OOP. I'm using the project to develop my understanding of this technique. Previously I would 'include' a functions folder that contains various php files labelled things like image.upload.functions.php and general.error.handling.functions.php etc.
However, this time I'm using classes wherever possible.
I have just read that in order to use a parents methods (in an extended class) you must run the parents constructor however I haven't done this and my project seems to work ok.
So.. I have a class called Form Validation
I have another class called Process Login that extends Form Validation.
My Form Validation class does things like test a password strength to make sure it is strong enough, check whether a user is in the database etc.
I extend Form Validation with a Registration class and a Forgotten Passowrd class.
Should I be putting:
parent::_construct();
..in the constructor of each of the extended classes?
Could someone explain 'simply' the reasons why we do OR do not do this? And whether it's something I should be doing?
Many thanks :-)
Here's the link to official documentaion: constructors and destructors
And here's the quote:
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).
Calling parent constructor is NOT necessary. And, in general, it is not called implicitly, if You have defined constructor in Your own child class. But, if in the parent constructor You have a nice piece of logic or functionality, that You don't want to lose, then call parent's constructor from the child's one.
You call parent::__construct() (watch that there are two underscores) if you want to reuse the functionality of the constructor from the class you're extending.
The reuse of code is a main reason for inheritance in OOP. So if there is any algorithm in your parent class that you want to use, you have to call parent::__construct().
If you're extending the parent's constructor, you also have to call it before (or after) your own additions, e.g. like this
class A extends B {
public __constructor() {
parent::__construct();
// Your own code
}
}
If you don't want to use any of your parents' constructor functions, you don't inherit from that parent constructor - but I assume that in most cases you want to.
The parent doesn't have constructor. In the child, I want to hook up some function with actions outside the class, so I need a constructor. Can I just add one in the child?
Yes you can.
If parent has one you may add in child with parent:: __construct();.
If the parent class does not have one, no need to use this code in child. just add __construct() method.
There are lots of examples of child classes calling overridden parent methods, most commonly parent::__construct(). In these cases, however, you are actually calling the parent from a concrete method in the child itself.
Is there a way that I can recursively call a method from the child to the first ancestor, using an inherited method?
If I use parent::methodName() in my abstract parent class it causes a fatal error saying 'Cannot access parent:: when current class scope has no parent'. Presumably this is because 'parent' is being evaluated relative to the abstract class itself, not the current class context.
Thanks...
I'm not quite sure what you're trying to do, but:
The parent keyword is evaluated relative to the class it occurs in. If the class that you use the parent keyword in doesn't have a parent, it obviously won't work.
If you want a child to use a parent's method, simply don't override that method in the child. It does not make sense to define a method in a parent that forces the child to call the parent's method, when all you need to do is simply not override the method in the child.
You can force the parent's method to be "the last word" by making the method final, the child won't be able to override it.
A child cannot call a "grandparent's" method, since the child does not know, or at least has no guarantee, that its parent has a parent in turn.
In practical terms that means:
If you are writing a class that does not extend another class, it doesn't make sense to use parent anywhere in it.
If you are extending a class and are not overriding a method, the parent's method will be used automatically (it is inherited).
If you are extending a class and are overriding a method, you may call the parent's method from that overridden method, which in turn may call its parent's method etc.
Maybe something like this? (untested)
foreach (get_class_parents($this) as $className) {
(new ReflectionMethod($className, __METHOD__))->invoke($this, $args...);
}
I hate when I see this kinda junk in code.