7

I have an abstract class "Class". The class Subclass extends Class. The Class abstract class has the following call:

is_readable('some_file.ext')

How do I force the children of the abstract class to look for the file in the folder they are in, instead of the folder of the parent abstract class, without overriding the method in the children?

I.e. if abstract is in

classes/abstracts/Class.php

and the child is in

classes/children/Subclass.php,

how do I make Subclass.php look for some_file.ext in classes/children/ instead of classes/abstracts, without explicitly defining it in the Subclass?

hakre
  • 193,403
  • 52
  • 435
  • 836
Swader
  • 11,387
  • 14
  • 50
  • 84
  • 2
    This question may have the answer you need. http://stackoverflow.com/questions/8364246/get-filename-of-extended-class – CLo Jun 20 '12 at 10:39

3 Answers3

16

You can use ReflectionClass::getFileName() to retrieve the filenames in which the subclasses were defined.

// In Superclass
public function isReadableFileInClassDir($file='somefile.ext') {
    $reflection = new ReflectionClass($this);
    $directory = dirname($reflection->getFileName()) . PATH_SEPARATOR;

    return is_readable($directory . $filename);
}

This works because $this no matter where it is defined will always refer to the instantiated class (and not it's parent even though the $this is found in the parent).

Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
0

You might be able to use something like the following:

is_readable(dirname(__FILE__).DIRECTORY_SEPARATOR.'some_file.ext');

The dirname method returns the directory of the file you pass it.

Take a look at Magic constants for more info on __FILE__

EDIT

In the child class include a member variable set to __FILE__ and reference that in the abstract class.

Child class

var $source_location = __FILE__;

Abstract class

is_readable($this->source_location.DIRECTORY_SEPARATOR.'some_file.ext');
acqu13sce
  • 3,789
  • 4
  • 25
  • 32
  • Nope. That still grabs the abstract's location. – Swader Jun 20 '12 at 10:34
  • It does. But like I said in the original post - I want to keep the file-loading call in the abstract, without ever having to repeat it in the child class file. – Swader Jun 20 '12 at 10:37
  • Thanks for the effort, but I don't think you understand fully - I don't want to edit the child classes at all in regards to this call. I want it defined in the abstract and never touched again. So that when another child is created, the developer doing it doesn't even think about this. – Swader Jun 20 '12 at 10:42
  • Fair enough, I think PhpMyCoder has the solution for you anyway, his is much better than mine – acqu13sce Jun 20 '12 at 10:42
0

I believe You have a good reason for saving classes and subclasses in different folders within Your file system, but consider Zend autoload or namespace based class loading - it will fail in Your case...

Instead of having this architecure:

classes
   | - abstracts
          | - AbstractClass1.php
          | - AbstractClass2.php
   | - children
          | - ChildClass1.php
          | - ChildClass2.php

consider this:

classes
   | - AbstractClass1.php
   | - AbstractClass2.php
   | - AnotherClass.php
   | - AbstractClass1
          | - ChildClass1.php
          | - ChildClass12.php
   | - AbstractClass2
          | - ChildClass2.php
          | - ChildClass22.php
   | - AnotherClass
          | - ClassA.php
          | - ClassB.php
          | - ClassB
                | - ClassBA.php

By this architecture it is totaly clear that ClassBA that is in path classes/AnotherClass/ClassB extends ClassB which extends AnotherClass. Now You can use namespaces (when using PHP 5.3 < ) or by naming Your classes like class AnotherClass_ClassB_ClassBA { ... } (Zend class naming convention) You can use Zend autoloader (e.g.)...

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • The system I have in place is PSR-0 compliant, so all PSR-0 autoloaders work well - namespace loading is in place and there are no problems. I only had this one problem which was answered, no more worries :) – Swader Jun 20 '12 at 11:09