defining a class/instantiating object inside a method of anotherclass

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default defining a class/instantiating object inside a method of anotherclass

    Ok, here's what I'm trying to do.
    I have a class. Inside the class I have a method that reads a text file.
    In the text file I have a class name and a file name where that class is
    defined. Now, I need to instantiate an object of that class, the problem
    being that at the time I find out the class name and the file name, I'm
    inside a method of another class. Well, let me just type up a little
    example:

    class MainClass {
    $obj = null;
    ...
    function init() {
    $lines = file($some_file);
    ... // parsing
    $kv = explode(":",$line);
    $className = $kv[0];
    $classFile = $kv[1];

    // somehow define className that's in classFile

    $this->obj = new $$className();
    }
    }

    I don't remember exactly how it was I used a variable with new, I think
    it was $$, but that's not the problem. The problem is that I don't need
    to include the class definition file inside a function, I just need to
    define a class using what's inside that file.
    Taking the whole thing outside of the class definition is not going to
    work - suppose I have many subclasses of this class, each with its own
    file, then for every class I'd have to provide a separate function
    outside the class definition to read the file.
    Think of it as creating a tree of objects where children objects' class
    names are taken out into a separate text file, so that at any time it
    would be easy to change the set of succeeding children without changing
    the parent class implementation.
    Any ideas? :)

    Lüph
    Lüpher Cypher Guest

  2. Similar Questions and Discussions

    1. Pass Class Object To FMS Using NetConnection.call Method
      Hello All, I have a custom class that defines several methods on itself to retrieve its data. This class object is then sent to FMS via the...
    2. How should I announce the list of delayed class/object/method loaders?
      I've been maintaining a list of object loaders for quite some time now: http://metaperl.com/article-pod/Catalog-lazy_loaders/lazy_loaders.html ...
    3. Assign inside a class method
      Hi, great language Exactly I want to implement a postfix increment, but I don't know how to assign inside the class method class Numeric def...
    4. Passing an Object Class from a method to a caller
      Hi -- On Fri, 12 Sep 2003, Michael Granger wrote: I think break (pre-1.8.0 or without explicit value in 1.8.0) evaluates to nil: $ ruby...
    5. Passing an Object Class from a method to a caller
      This is a multi-part message in MIME format. ------=_NextPart_000_004C_01C37882.24993D90 Content-Type: text/plain; charset="Windows-1252"...
  3. #2

    Default Re: defining a class/instantiating object inside a method of another class

    On Tue, 19 Oct 2004 04:07:11 GMT, Lüpher Cypher wrote:
    > Taking the whole thing outside of the class definition is not going to
    > work - suppose I have many subclasses of this class, each with its own
    > file, then for every class I'd have to provide a separate function
    > outside the class definition to read the file.
    No, just include the class definition file anywhere, even inside the
    function block. As PHP is parsed and compiled before it's executed, class
    definitions are valid for whole program, regardless of where they are
    defined.

    Berislav
    Berislav Lopac Guest

  4. #3

    Default Re: defining a class/instantiating object inside a method of anotherclass

    Berislav Lopac wrote:
    > On Tue, 19 Oct 2004 04:07:11 GMT, Lüpher Cypher wrote:
    >
    >
    >>Taking the whole thing outside of the class definition is not going to
    >>work - suppose I have many subclasses of this class, each with its own
    >>file, then for every class I'd have to provide a separate function
    >>outside the class definition to read the file.
    >
    >
    > No, just include the class definition file anywhere, even inside the
    > function block. As PHP is parsed and compiled before it's executed, class
    > definitions are valid for whole program, regardless of where they are
    > defined.
    >
    Cool, I didn't know that. Thanks!
    Lüpher Cypher Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139