PHP Namespaces - Extend a class without extending an extended class - php

We are looking to build a system with core classes and the ability to extend these core classes and are looking in to using namespaces.
The problem we are having is working out if we can extend an extended class without extending the class that it extends from
For example, if we have folders and files as below
shared/classes/Entity.php
shared/classes/DatabaseEntity.php - Extends Entity.php
shared/classes/User.php - Extends DatabaseEntity.php
classes/ - Holds classes which extend from the shared classes
If we wanted to create a custom DatabaseEntity class without creating a custom User class , is this possible?
The way I understand this is that the User class will be looking in the shared namespace to extend the DatabaseEntity class but as we have extended the DatabaseEntity class, it needs to look at the top level classes directory
Example of shared/classes/User.php
namespace shared;
class User extends DatabaseEntity {
}
Example of shared/classes/DatabaseEntity.php
namespace shared;
abstract class DatabaseEntity extends Entity {
}
Example of classes/DatabaseEntity.php
namespace custom;
use shared\classes\Entity;
abstract class DatabaseEntity extends Entity {
//Some custom functionality to extend shared/DatabaseEntity
}
So if we didn't want to change the User class to say
use custom/DatabaseEntity
Then is this possible?
Hopefully that makes sense
Thanks in advance for any help

If you don't want to add to User class
use custom/DatabaseEntity
and you want to extend custom/DatabaseEntity
you may just change class declaration from
namespace shared;
class User extends DatabaseEntity {
}
to
namespace shared;
class User extends \custom\DatabaseEntity {
}
if you want to extend \custom\DatabaseEntity.
If it's not want you want to achieve I cannot understand your problem - you ask two questions.
You asked
If we wanted to create a custom DatabaseEntity class without creating
a custom User class , is this possible?
The answer is - yes, you just created it in your example. You created custom DatabaseEntity class without creating custom User class.
But if you want to achieve:
it needs to look at the top level classes directory
you need to tell User class to extend specific class - so you will need to extend using fully qualified class or import namespace using use and creating alias

I don't know if I understand you well, but you want to create CustomDatabaseEntity class that will extend DatabaseEntity and you don't want that CustomDatabaseEntity extends User class.
It's of course possible. You can create as many child classes as you want. As User class is defined that it extend DatabaseEntity class it will even don't know that you created CustomDatabaseEntity

I also think that you are using it a bit wrong. If DatabaseEntity have anything common with database and not with User itself, you should rather create Interface DatabaseEntityInterface, those two DatabaseEntity classes should implement interface
and then in User class you should pass it as constructor argument
class User {
protected $dbi;
public function _construct(DatabaseEntityInterface $dbi) {
$this->dbi = $dbi
}
}
and later you can pass to User class either class for shared folder or the one from classes

Related

Override object creation with same class in another namespace

I'm trying to trick PHP into taking a class from another namespace when trying to create a specific class.
I have two class called "page", the first is in the Core namespace:
namespace Core;
class Page {...}
The second inherits from Core\Page, but adds a few things. It is in the Addons namespace.
namespace Addons;
class Page extends \Core\Page{...}
The reason I want to do this is because I want to build my system with an easy addon engine. Whenever I want, I can add a line in an XML file that tells the autoloading function to take the class in the addon namespace instead of the core namespace.
However, when I try to do this :
spl_autoload_register('loadClass');
public function loadClass(string $className)
{
if (Addon_exist_and_is_registered($className))
{
require "/Addons/$className.php";
}
else
{
require "/Core/$className.php";
}
}
$page = new \Core\Page(); <-- error here
I get an error saying that the class \Core\Page cannot be found in the file Addons\Page.php. This is normal behaviour since the class is not in the same namespace and as such, the fully qualified name cannot find the right class.
Is it possible to trick PHP into thinking that a child class in another namespace is actually the right class? I tried this for the addons class;
namespace Core;
class Page extends \Core\Page{...}
But it breaks the inheritance as you cannot inherit yourself.
Ignore that the classes have the "same name". Because they don't. One class is called Core\Page, the other is called Addons\Page. Those are their names, their fully qualified names to be exact. It's as much a difference as Foo and Bar. If you tell PHP to instantiate Core\Page, then it's going to do that; you can't "trick" it into instantiating Addons\Page, since that's an entirely different class name.
Don't try to "trick" anyone, make your system actually extensible and explicitly allow overriding of class names:
$class = 'Core\Page';
if (...) {
$class = 'Addons\Page';
}
$page = new $class;

How does extends work in classes?

I've been trying to build a PDO extension, and I wanted to have special classes in different files but I wanted to have them all link to the same original class.
I have worked with some Frameworks and I see that they use the extends class keyword, and I thought that it added the class you are making to the class that you have given.
Some code I have tried is:
class PSMQuery extends PSM {
// Functions and Jargon
}
I tried making an object for the original PSM class:
$psm = new PSM(/*Information*/);
But when I call the $psm variable like $psm->functionInTheExtendedClass it comes up with an error saying that it was an undefined method when I called it.
Am I using the extends keyword incorrectly?
Am I using the extends keyword incorrectly?
You use it correctly, but it works the other way around.
If PSMQuery extends from PSM, this means you can access and use stuff from PSM in PSMQuery, but not the other way around.
I think to understand it you can use a good example:
class twoRoomApartment extends building { }
So now you can think logical and already see, that a two room apartment probably extends from a building and not the other way around.
Means now in your code, you just create an instance of PSMQuery.
It's inheritance.
Parent : PSM
Child : PSMQuery
When you use extends you are extending parent class functionality and creating child class.
Your child class will inherit all the parent class functionality.
Parent will not get child class functionality.
So when u r trying to create object of parent class it doesn't know child class functionality.
You need to create object of child class and then you can access methods from parent class.

What is the purpose of empty abstract classes in Kohana 3?

I haven't used abstract classes much in practice, though I understand what they are : a way to dictate to subclasses which methods must be implemented.
I just put a Kohana project through Doxygen and can see the class hierarchy in its entirety. I see that, at the top of the chain we have a factory:
abstract class Kohana_Model {
public static function factory($name){
// Add the model prefix
$class = 'Model_'.$name;
return new $class;
}
}
Inherited directly below that, we have an empty abstract class:
abstract class Model extends Kohana_Model {}
... And below that, there are three inherited classes: Kohana_Model_Database, Kohana_ORM, and Model_Foobar.
Would someone please explain the programming reasoning for this - what is the purpose of having an empty abstract class this high up in the chain? (and, at all?)
Why not have Kohana_Model_Database, Kohana_ORM, and Model_Foobar inherit directly from Kohana_Model, when there is (apparently?) no other branching or inheritance going on between Model and Kohana_Model?
Answers you're seeking for are Cascading File System and Transparent Extensions.
It allows you to create a model by calling
class News_Model extends Model
by default, and that will automatically then extend Kohana_Model and things will be hunky dory.
It also lets you extend Kohana_Model by creating your own Model file
class Model extends Kohana_Model
which overrides the abstract Model class, and allows you to add custom functionality. Then, when you upgrade your Kohana version to (say) 3.4, your extended Model doesn't get overwritten by the new Kohana files.

PHP: multiple namespace in same class?

I'm building a class that interacts with an API. Say the API has a "get_something" method for "foo" objects and "bar" objects. I want my class to expose a "get_something" method, but be able to distinguish if it's for "foo" or for "bar."
What's a good solution for me? Can I create a class that has multiple name spaces? Would that be a good idea?
Maybe I should have nested classes?
you should look at namespaces as packages in java. a class belongs to 1 package only
what you can do though is have
namespace A_NS;
class A { ...}
namespace B_NS;
class A extends \A_NS\A {};
in that case class A will exist under namespace A_NS, and another class A' will exist under B_NS, which will extend class \NS_A\A;
you could check an object's class, or implement some identifier inside to distinguish.
overall, i would recommend that the design of your system will treat each class as if it belongs to 1 namespace only.
Two ways to do this, you can use an interface (http://us3.php.net/interface), which ensures that classes implementing the interface will have specified methods. Or create a parent class with abstract method (http://us3.php.net/abstract) get_something which will force any child classes to also have the class. Then you should be able to do a get_class($object) to determine which class was instantiated.

Zend Frameworks - separation of content return type?

I am about to develop a set of custom Ajax/rss/etc functions, both abstract, and then ones to be used in controllers. I was thinking of separating these methods based on return type.
I have a controller which will be enormous if I don't break down the logic.
I was thinking maybe a module - like
Modules/Admin/AnalyticsController
Modules/AjaxApi/AnalyticsController
Modules/RssApi/AnalyticsController
Any advice is appreciated!
Have you considered using or overriding or creating your own Context Switcher. You can read more here http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
This has the features you need without needing to create new controllers for each action.
You can create your own abstract class with common methods. But in PHP you can to inherit only 1 class. Make your abstract class extends from Zend_Controller Action. Example code:
abstract class AjaxRssEtc extends Zend_Controller_Action
{
// code
}
class Ajaxapi_AnalyticsController extends AjaxRssEtc
{
// code
}
And your controller Ajaxapi_AnalyticsController will have methods from Zend_Controller_Action and your abstract class.

Categories