0

I'm having troubles finding a class name after extending other classes. Here are the relevant classes:

class Home extends Controller {
       public function test() {
           Example::all();
       }
}


Class Example extends Active {
    //Variables
}

Class Active extends Database {
     public function all() {
        //This is where I need to store a variable containing the class name Example.
     }
}

I'm trying to retrieve the name of the Class Example in Class Active from when it is called from class Home, however I've found it impossible to do this so far without adding an extra argument (which I don't want to do).

5
  • 1
    The way you do it, just go procedural, why do you bother with classes? Commented Jul 23, 2011 at 16:46
  • @Itay Moav - Why not go OO???? Commented Jul 23, 2011 at 16:51
  • 1
    @Matt I guess what Itay wants to say is that the OP is doing class based programming and in that case OP can just as well do procedural because class based programming isnt object oriented at all. Commented Jul 23, 2011 at 16:59
  • @Gordon It looks at though OP is attempting to create a larger object oriented application. In which case why would he want to go procedural? Commented Jul 23, 2011 at 17:41
  • 1
    @Matt using classes doesnt make an application object oriented. If you need statics and LSB you are using class based programming and thats much closer to procedural than oo. See the last link in my answer. Commented Jul 23, 2011 at 18:03

2 Answers 2

2

You are looking for

Example (demo)

class Home  {
       public function test() {
           Example::all();
       }
}
Class Example extends Active {
    //Variables
}
Class Active  {
     public static function all() {
        var_dump( get_called_class() );
     }
}

Note that I have changed the signature of Active::all to static because calling non-static methods statically will raise E_STRICT warnings. In general, you want to avoid static methods and Late Static Binding.

Sign up to request clarification or add additional context in comments.

11 Comments

about "avoid static..." after so and so years it dawned on me that I rather have to write much less, so for PHP for example I would rather have most classes have a static API, but still be able to use them directly If I need something more comples, a simple example: $id = DlClass::getInstance($tname)->id as opposed to: $DL = new DlClass($tname); $id=$DL->id;
@Itay well, I think the linked article explains pretty well why you win nothing with statics.
this article is just an opinion and he brought (in my private opinion) bad examples, i.e. good examples why static is bad. Every coin has two sides (otherwise the goto would have disappeared from the face of the earth). Assume every class you have also has the following static property+ static method. Would it save you some typing in the future? public $LastInstance=null; static public function getInstance($params){ //some logic whether to instantiate a new object or return the last instance; return static::$LastInstance; } public function __construct(....){...}
@Itay No, it wouldn't. What you show would be a Singleton (if it had a private ctor). Singletons are Anti Patterns. They mix creation graph with collaborator graph and thus violate SRP. They are harder to test due to the tight coupling and introduce global state and side effects into applications. They also violate DIP. Singletons hurt your architecture. The above holds true even with the public ctor. It's bad design.
The above class has nothing to do with Singletons (I knew you would write that :-) ) 1. I can have as many as I want. 2. I can use normally 3. All I have is a short cut that ,manages for me some common usages of this code, that's it And that is my only point. I use some static methods for shortcuts of the most common task an object will be used to, right? would not make sense to see the following code 1000 times in your code: $T=new F(p); $T->u()->r()-k();$T->g++; echo $->newG(); If I can do a shortcut like $T::showNewT(p);KISS is by FAAAAAR the most important DIP.
|
0

have you tried with ReflectionObject class from PHP ? Have a look to this method

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.