PHP OOP Function Precedence - php

In OOP PHP, if I have a function defined in the parent class, and a modified version in the child class, and I call it from an instantiated object of the child class, will it use the child class's version of the function?
I am pretty sure it will, but I just wanted to double-check, as there's no way for me to check within a running application.

Yes, it absolutely will.
If you want to use the parent's version too, you must call parent::the_func() within the child's override of that function.
You must also call a parent's constructor if you override the constructor in the child. It is not called automatically.

Related

Disable old-style constructors (PHP4-)

I am working on an MVC framework in PHP.
I have several controller classes called "index" with methods called "index" within them. The classes do not have __construct() methods.
Inevitably, this is resulting in PHP calling the "index" method as the constructor instead, using the old PHP4 convention of the constructor being the method with the same name as the class.
Is there any way to disable this behaviour or do I have to define an empty __construct() to prevent it? (Or just change my own coding style so I don't have methods with the same name as their classes.)
I want PHP5 to stop parsing for the PHP4 constructors essentially.
Ilmiont
I have been down the same road with MVC frameworks before and I have never heard of an index() function being called like this. Upon instanciation it will call the __constructor if present or do nothing. When calling your controller you should be geting the class and method name and checking if they exist and if they do then instantiate it. However if you have the same general setup as me you should have a registry storing all of your variables being passed to the controller when you create an instance, then when/if the view is called from index it should pass the altered registry to the view

php class inheritance - is it necessary to run constructor on parent(s)

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.

oop php constructor access modifier, which one to use?

I've just started experimenting with OO PHP, but there's one basic principle I don't really uderstand that well, and don't find too much info on it.
When creating a __construct() method, why would you want it to be public, when it's specifically a constructor for that class?
When would you want to call a constructor outside the class?
To me, it seems using a protected constructor is good practice, right?
I know this is basic OO stuff, but I don't find any info directly on it, specifically for constructors.
The __construct (not "__constructor") method is the one called when you do new MyClass(), i.e. when you instantiate the class. The constructor needs to be public, unless you only want to instantiate the class from within itself. If the latter, you need at least one other public static method you can call in which the class will instantiate itself, otherwise you're unable to create any instance of it.
Whenever you create a new instance of a class, the constructor is called. If the constructor is not public, no other code can create an instance of that class.
Hence, if you want to create instances of the class, make the constructor public.
A constructor is always only part the class it is defined in, I don't understand what you mean by "when it's specifically a constructor for that class".
To clarify:
The only way to invoke the constructor is with new Class(). There is no other way to invoke it. __construct is a magic method and there is no way to explicitly call a magic method.

Can I recursively call a method from child to parent, using an inherited 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.

Calling a parent classes method vs. calling a static method

To call a parent class (it has been instantiated) method I use
parent_class::method(); //tested it works
to call a method with in the instantiated class I am in I use
$this->method(); //tested it works
However if I call a static method from any class I use
parent_class::static_method(); //tested it works
I guess this makes since b.c. there is only one copy of a method per class, whether it is instantiated or not?
Can some one validate or provide insight to this. I just want to verify that the call method is the same for both static methods from any class and calls to a parent classes method.
Seems a bit strange.
The syntax is correct. Not exactly sure what your question is. If you wanted to call a static method defined in the child from within the child, you could use self::static_method() or $this->static_method(). Either would work.

Categories