SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member AlexBrina's Avatar
    Join Date
    Jul 2004
    Location
    office.bh.mg.br
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to get the subclass name with a static superclass method?

    Hi,

    PHP Code:
    class SuperClass
    {
      function 
    echoName()
      {
          echo ???;
      }
    }

    class 
    SubClass extends SuperClass{}

    SubClass::echoName(); 
    What should replace the '???' to get the name of the SubClass echoed.

    If I use the __CLASS__ in the SuperClass it echoes the SuperClass name, get_class($this) isn't an option because it's not an instance (and $this isn't defined). ReflectionClass needs the classname as an argument.

    So guys, how can I echo the SubClass name using a static call to a SuperClass method? I'm afraid it's not possible, thank you guys anyway.

    alexbrina

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is that hard to get, perhaps you should evaluate the design which is causing you to want that information.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Member AlexBrina's Avatar
    Join Date
    Jul 2004
    Location
    office.bh.mg.br
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heheh, I was afraid of getting this answer, the design may be the problem indeed, but I'm feeling so happy with it that I decided to give it a try. Let me explain:

    I am designing a simple persistence abstraction layer.

    So I have a general "dataobject" that represents a instantiated object (a table row). It doenst belong to a specific table, it's really general. It has these properties:

    current = array of type column=>value for the current values
    original = same array, but the values here are the last ones retrieved from persistence
    status = can be NEW, NEWMODIFIED, DATAMODIFIED, NOTMODIFIED
    deleted = TRUE | FALSE
    modifiedColumns = array of modified columns
    mapperName = name of the mapper that will handle the object persistence or retrieve

    ... and it has the methods to access these properties including
    get( $column ) and set( $column, $value ) and save() that instantiate the mapper using:

    $mapper = new $this->mapperName;
    $mapper->save($this);

    In the other hand I have the "datamapper" which is responsable for persisting and retrieving dataobjects instances, it has the property:
    columns = array of columns definitions, like:
    array( 'id' => array( pk => TRUE|FALSE, auto => TRUE|FALSE, type => 'int'... etc)).
    And it has the general methods: create($mapperName), retrieve($where), plus the methods to build the SQL commands to persist the object based on the state of the $dataobject instance, like: insert($dataobject), update($dataobject), etc ... and save($dataobject) which detects what command should be built, using the $dataobject->status and $dataobject->deleted properties. (what a mess!)

    What I want in the code is:
    $p = person::create(); // which does: return new dataobject('person'); the constructor parameter here goes to the mapperName property of the dataobject
    $p->set('name','Zack');
    $p->save(); // instantiate the mapper and call the mapper save method, to do INSERT

    $p = person::retrieve(' id = 1 '); // builds the select based on the person's columns definition.
    $p->set('name','Isack')
    $p->save(); // builds the UPDATE

    Each mapper extends the datamapper to set the columns property for each class, like:
    class person extends datamapper
    {
    static columns = array(...)
    }

    to do that I need to declare these methods in the person class, like:

    function create()
    {
    parent::create('person');
    //OR
    parent::create(__CLASS__);
    }
    .
    .
    .

    If there was a way to discover the classname of a SubClass using a method from SuperClass I would not need to add these declarations (I'm lazy), and others that I haven't listed here, so my mapper classes would be very easy to build, just the column definition would be needed.

    Sorry for the mess, but maybe it's "understandable"
    Alex Brina
    "...sempre q eu tirar a cabeça fora d'agua eu dou um alô..." JC

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AlexBrina
    Hi,


    So guys, how can I echo the SubClass name using a static call to a SuperClass method? I'm afraid it's not possible, thank you guys anyway.

    alexbrina
    Correct. It's not currently possible. Feature has been requested many times. Maybe in PHP 6?

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not quite sure why you want create/retrieve to be static don't you end up instantiating an object anyways?

    If I remember correctly, this problem usually comes up when people try and implement ActiveRecord in PHP. Its impossible to implement the static findAll method so you have to instantiate dummy objects...

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jnaslund
    If I remember correctly, this problem usually comes up when people try and implement ActiveRecord in PHP. Its impossible to implement the static findAll method so you have to instantiate dummy objects...
    The problem comes when people use static methods. Call it the wrath of God.

  7. #7
    SitePoint Member AlexBrina's Avatar
    Join Date
    Jul 2004
    Location
    office.bh.mg.br
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I can see it's not possible today, but I still think it's useful and would be nice in next php version. It's not the first time I desire this feature.

    Btw, talking about the persistence layer, now it seems to me that there are 3 different things here:

    1. the "row" instance with its state, I call it dataobject. It's amorphous and don't need to know about table's definitions.

    2. the "table" mapper knows the table's definitions, I call it datamapper. It knows which columns are pk's, fk's, if column is auto-generated, column type, all metadata needed to handle the data stored in a dataobject. That's what most guys like doing with XML files. I'm happy with php arrays!

    3.the "database" gateway (good name?) builds the commands, I'll call it datagateway(?). This guy will generate the commands to persist or retrieve data based on the datamapper and/or the dataobject. It could be a singleton. I think it will give me good flexibility due to db access isolation.

    I'm not trying to build a "complete" abstraction system, but those simple CRUD things can be easily done with these classes. The problem now is, where should i put the "nice" queries plenty of tables and subqueries, the "business logic" that people call "model" nowadays? I don't want to lose the flexibility acquired from db access isolation, maybe I should come up with a 4th class. Don't know.

    The fact is, a clear OO design for enterprise systems can be done, and is beautiful, in the Analysis/Design phase, but Implementation is not beautiful. Dealing with everything as an object causes a big overhead, the good old UPDATE affecting N rows with a single query makes much more sense than zillions of foreachs doing one command at a time, or a SELECT retrieving assoc arrays with data from 3 or 4 different tables without the need to instantiate an object of each type, for each row. We can't throw away all the DBMS efforts in optimizations, not even talking about network load. When I read about pure OO design, Rails' ActiveRecord, Propel, Hibernate, etc... I think "I wanna do it this way", but when I see articles that shows the complexity behind the curtains, like this one http://martinfowler.com/articles/dblogic.html I get biased. Well, I wonder with a better world with simplicity everywhere, it's still very far from us (or maybe just me).

    I went totally off topic here, but I gotta take it off my chest!
    Alex Brina
    "...sempre q eu tirar a cabeça fora d'agua eu dou um alô..." JC


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •