Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top