Is it possible to create a "Shortcut" class to lead to another?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    Is it possible to create a "Shortcut" class to lead to another?

    I have a data abstraction class that can get data from 3 different "Types" as they all have the same properties:
    Code:
    class Type {
      private $Tables = array('type1','type2','type3');
       __construct($type,$id) {
          $db->query('SELECT * FROM `'.$this->Tables[$type].'` WHERE `id`='.$id);
          }
       }
    Ignoring the obvious faults in the code thats the basic gist of what I have.

    Can I do something like this to kinda make a "Shortcut" class?
    Code:
    class FirstType {
       __construct($id) {
          return new Type(0, $id);
          }
       }
    The reason I say this, is because in my methods, I'm dynamically calling classes through a function of mine:
    Code:
    function getClass($class) {
       if ( class_exists($class) )
          return new $class();
       else
          return false;
       }
    Doing it to save on a large switch block.

    So back to the question, can I do this? Or what can I do?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I am not quite sure what you mean by types, are they DB types ie MySQL, MsSQL?
    But firstly a constructor can only return a class object, so your first idea won't work that way.
    Also could your function getClass($class ) not be better served with __autoload? Not everybody likes this I agree.
    Maybe what you are trying to do already exists by using Abstract or the other similar one.

    Comment

    • Samishii23
      New Member
      • Sep 2009
      • 246

      #3
      By types, I mean proprietary to my project. Just internal data types for my project. They all share the same database columns so I figured it was easy to group them together.

      What I was doing was this:
      Code:
      class dm { // main program class
         private $dcNames = 'dataClass1,dataClass2...';
         }
      $_dc = explode(',', $this->dbNames);
      $dc = getClass($_dc[$section]);
      Maybe not the best method but a little easier.
      The getClass function I'm using is exactly the same as shown above. I saw the method through a Polymorphism tutorial. Though I don't believe my code gets close to what is written there. What they did was an easier way to do:
      Code:
      switch($class) {
         case 'dataClass1': $dc = new dataClass1($id);
         case 'dataClass2': $dc = new dataClass2($id);
         // ...
         }

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        very much possible

        follow the example

        taken from link

        Code:
        <?php
        $instance = new SimpleClass();
        
        // This can also be done with a variable:
        $className = 'Foo';
        $instance = new $className(); // Foo()
        ?>

        Comment

        • Samishii23
          New Member
          • Sep 2009
          • 246

          #5
          @johny10151981: Using the variable to dynamically instance a class is what I am doing. The question is stemming from the first post.
          Code:
          class Type {
            private $Tables = array('type1','type2'); // database tables, but exact same columns
            function __construct($type,$id) {
              // content
              }
            function stuff() { ... }
            }
          
          // Shortcut class
          class FirstType {
            function __construct($id) {
              return new Type(1, $id);
              }
            }
          
          $WantFirstClass = new FirstClass(10);
          $WantFirstClass->stuff(); // Error here
          Should I just extend the Type class? Or what? cause the Type class constructor sets properties and what not, so how would I do that? parent::__const ruct() ???

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Should I just extend the Type class? Or what? cause the Type class constructor sets properties and what not, so how would I do that?
            You can't explicitly return anything from a constructor because a constructor impicitly returns a class object, that is the class in scope.
            Code:
            function __construct($id) { 
                return new Type(1, $id); #Wrong
            So yes, I would say FirstType needs to extend Type to access stuff() which needs to be at least protected.
            And you do need to explicitly call parent::__const ruct if the child class defines a constructor.
            But I can't see what you gain from this idea.
            I would be thinking along the lines of an Abstract class Type{} with common member variables and methods,
            and variations thereof in child classes.

            Comment

            • Samishii23
              New Member
              • Sep 2009
              • 246

              #7
              The reason why I'm doing this, is because I have a few data classes that I'm doing to call dynamically, and I am just putting the names into my string reference. Not particular sub types within one of the classes. Or I could rewrite the same exact class 3 times over (69 lines each) or just one with 3 more lines that takes a type parameter and dynamically sets itself up with the right data. If that makes any sense...

              Comment

              • JKing
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                Hi there,

                It sounds to me like you want to extend the class.
                PHP Class Examples

                Comment

                • Samishii23
                  New Member
                  • Sep 2009
                  • 246

                  #9
                  Yep, extending worked! :)
                  Thanks for the help!

                  Comment

                  Working...