I have stumbled upon an interesting problem/bug that I eventually solved, but the solution is not what I expected or would like it to be.
The setup is simple. There is an abstract static class Factory, which has a reference to a singleton (Registry) as well as two static properties, model and table.
There are a number of static classes building upon/inheriting from this abstract class and they all have an init() method in which they set the model and table properties. Of course, since model and table are static they can only have one value which is the same for all the child classes of the abstract Factory class. This is the problem/bug.
However, my aim is to have each child class have its own model and table so I am forced to declare model and table in each child class as a static property. This seems a bit cumbersome (and not very DRY), but it seems to me that this is the only solution if I want to have (1) the classes inherit from the abstract Factory class and (2) remain static.
Is my assumption correct or is there another approach that I am missing?
The goal is to have different values for $model and $table in different static child classes?
Assuming that is the case, I don't see how You could possibly accomplish that without explicitly defining them different in each child class.
I don't think it is cumbersome. I see it as a good practice - by keeping functionality where it belongs.
Related
In real world project, we had a pretty long PHP class, and during refactoring it was broken into 2 smaller classes, ClassA and ClassB, where ClassA extends ClassB (just an example names). There were some issues with legacy code, so we had to do that way.
In ClassB we call some methods from ClassA (they're not overriden, I mean, methods doesn't exists in ClassB). Is that a bad idea in OOP?
Edit: obviosly, in the rest of code we always initiate only ClassA. But wondering is calling parent class from the child a big "NO" in OOP terms?
What you're describing is a common pattern in OOP. The parent class should be an abstract class, so it can't be instantiated by itself, only the child classes can be instantiated.
The child classes are all required to implement methods that aren't in the parent class, but some methods are common to all the classes, and they're implemented in the parent. These common methods then defer to the children for some implementation aspects that are child-specific.
I know the concepts of Abstract Class and Interface Class.
But I want real time example which explains both the concept in our design.
Can anyone help with this?
Both have little different scenerios...
Abstract classes also contains method definition as you know interface does not. But if you defines abstract class you can not inherit more than one classes from child class. But In case of interface you can do that and implement the methods declared inside the interface.
These scenerios are used in the software development as we just declared the signature of the method and then user can write his.her own code inside the method by overriding it.
Here I am mentioning some point what we can do with abstract class and interface
In abstract class you can define abstract methods which should be public or protected. but in Interface you can define public abstract method only.
In abstract class you can define data members and constants but in interface you can define constants only.
In abstract class you can define body of method and you can inherit in sub class, but in interface you can not define body of method.
you can implement multiple interface in a sub class, but you can not inherit multiple abstract classes in a sub class.
Example
In Major Frameworks, uses interfaces for different types of data source class. If we want to make data source class for new introduced database then we must have to implement their interface in our class so it make well maintained code and methods parameters.
Looking for a little guidance here.
I know that private methods are not polymorphic and so it doesn't make sense make abstract functions of them, but I do want to somehow enforce that a child class of a base class always declares a private method.
Am I doing something wrong or is this okay? I know you can't do multiple inheritance so maybe traits are the way to go, but I don't know if I can enforce them in Php.
I was thinking, maybe I can have an abstract base class have an abstract protected method, and then have that class be final since the function has to be protected? The only downside is that I could go farther with this though.
I just want a child class to always have a base method that only it can access, but I'm not sure how to do that without making it final.
EDIT:
Situation context is I have a child classes that are like flyweights that inherit from a base abstract that do type checking (because php is dynamic) and also want to do a final check for required variables. Every child class has to do this and does it the same way, but it should be called by anything else.
EDIT Idea:
I was thinking I could just write an interface for the base class with a function to do the final function (in this case render), and inside function, I could call the error checking. The problem with this is that I could not access new attributes that I newly create in the extended functions because the base class function wouldn't know about them. Then I'd be stuck with putting them in an array or something which defeats the purpose of making the child class anyway. Also, if I did that, I couldn't be sure that the function didn't get redefined without making it final so that I could no longer extend it. Idk here. Please help if you have ideas.
This is less of an issue, and more of a best practice question - I know these are subject to opinion, but I'm sure there must be a standard convention for this particular problem.
Let's say I've got two classes, Account and Associate.
Account contains several methods which are useful to Associate, and so naturally I'd extend the Account class to Associate.
However, an issue of course arises when I have two methods with the same name e.g. create().
So far to counteract this instead of extending the parent class, I've been instantiating it as a variable in the child classes __construct() method, and then calling methods through that, e.g. $this->Account->create();.
Is there another way, e.g. a norm for using an extended classes methods while still having a method of the same name in the child class?
Any answers would be greatly appreciated!
Account contains several methods which are useful to Associate, and so naturally I'd extend the Account class to Associate.
No, this is not naturally, you are misusing inheritance. Or would you say that an associate is an account? If it has an account, use composition. If not, but the account class has methods that are useful for the associate class, they probably shouldn't be in the account class at all. Extract them to one or more other classes which can be used by both, Account and Associate.
The child class method will be called using $this if you are within the child class methods. If you want the parent method though, you'd call parent::create(); from within the child method.
But that is if you absolutely need to extend the Account class, which sounds unnecessary. If the "Account" class has public methods then you don't need to extend it, you can just call them after instantiating the class:
$account = new Account();
$account->create();
Or, if it is a public static method:
$creation = Account::create();
I have three classes which basically do very similar things;
Store a record of an uploaded file.
Move and upload the file.
Set the status of the record to active or revoked.
One of the classes has an additional update method. Each class references a different table in the database because although some of the fields are common, there are a couple of fields extra in some of the tables.
As quite a lot of the functionality is common I think it may be best to extend a base class rather than duplicating a lot of the functionality.
My only quarrel is the construct function on the base class. As some of the fields in each table are additional I'm concerned this will prevent a base class.
I have thought of using an abstract class as this will allow me to extend on functionality whilst maintaining most of the things in one place. But it's the problem with the construct. Can I have an abstract class with no construct?
Any ideas?
If I understand you: You can have any class without __construct(). And you can always overwrite every method (except methods, that are declared as final) of a parent class in a child class.
I am sorry but I guess I do not completely understand your question. Anyway in inherited class you can always override the constructor and within it invoke the superclass constructor plus further stuff if needed.