How to instantiate class (from array) inside the loopDeleting an element from an array in PHPFatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)When to use static vs instantiated classesHow do I get a YouTube video thumbnail from the YouTube API?List of Big-O for PHP functionsHow to Sort Multi-dimensional Array by Value?Instantiating class by string using PHP 5.3 namespacesTraits vs. InterfacesReference - What does this error mean in PHP?Instantiate global class inside another class

How to drill holes in 3/8" thick steel plates?

How did the hit man miss?

Referring to different instances of the same character in time travel

Is Trump personally blocking people on Twitter?

Why did Harry Potter get a bedroom?

Why do people keep referring to Leia as Princess Leia, even after the destruction of Alderaan?

Is anyone advocating the promotion of homosexuality in UK schools?

Would dual wielding daggers be a viable choice for a covert bodyguard?

How to md5 a list of filepaths contained in a file?

Are randomly-generated passwords starting with "a" less secure?

Is "I do not want you to go nowhere" a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?

Managing and organizing the massively increased number of classes after switching to SOLID?

Do you know your 'KVZ's?

For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?

How to loop for 3 times in bash script when docker push fails?

As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?

Why was hardware diversification an asset for the IBM PC ecosystem?

How can I effectively communicate to recruiters that a phone call is not possible?

Why do players in the past play much longer tournaments than today's top players?

How can a dictatorship government be beneficial to a dictator in a post-scarcity society?

What's the minimum number of sensors for a hobby GPS waypoint-following UAV?

Print the last, middle and first character of your code

Combining latex input and sed

C program to parse source code of another language



How to instantiate class (from array) inside the loop


Deleting an element from an array in PHPFatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)When to use static vs instantiated classesHow do I get a YouTube video thumbnail from the YouTube API?List of Big-O for PHP functionsHow to Sort Multi-dimensional Array by Value?Instantiating class by string using PHP 5.3 namespacesTraits vs. InterfacesReference - What does this error mean in PHP?Instantiate global class inside another class






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to instantiate class inside the loop, I've tried doing this:



<?php
use AppCaves_demographic_info;
use AppCaves_current_uses;
use AppCaves_flora_outside;

public function get_page4_contd_data($id)
$tables = [
'Caves_demographic_info', 'Caves_current_uses', 'Caves_flora_outside',
];


for($index = 0; $index < count($tables); $index++)
$$tables[$index]."_model" = new $tables[$index];



?>


It produces the error 'Class class_name not found', Is it possible to do that inside the loop?










share|improve this question




























    0















    I want to instantiate class inside the loop, I've tried doing this:



    <?php
    use AppCaves_demographic_info;
    use AppCaves_current_uses;
    use AppCaves_flora_outside;

    public function get_page4_contd_data($id)
    $tables = [
    'Caves_demographic_info', 'Caves_current_uses', 'Caves_flora_outside',
    ];


    for($index = 0; $index < count($tables); $index++)
    $$tables[$index]."_model" = new $tables[$index];



    ?>


    It produces the error 'Class class_name not found', Is it possible to do that inside the loop?










    share|improve this question
























      0












      0








      0








      I want to instantiate class inside the loop, I've tried doing this:



      <?php
      use AppCaves_demographic_info;
      use AppCaves_current_uses;
      use AppCaves_flora_outside;

      public function get_page4_contd_data($id)
      $tables = [
      'Caves_demographic_info', 'Caves_current_uses', 'Caves_flora_outside',
      ];


      for($index = 0; $index < count($tables); $index++)
      $$tables[$index]."_model" = new $tables[$index];



      ?>


      It produces the error 'Class class_name not found', Is it possible to do that inside the loop?










      share|improve this question














      I want to instantiate class inside the loop, I've tried doing this:



      <?php
      use AppCaves_demographic_info;
      use AppCaves_current_uses;
      use AppCaves_flora_outside;

      public function get_page4_contd_data($id)
      $tables = [
      'Caves_demographic_info', 'Caves_current_uses', 'Caves_flora_outside',
      ];


      for($index = 0; $index < count($tables); $index++)
      $$tables[$index]."_model" = new $tables[$index];



      ?>


      It produces the error 'Class class_name not found', Is it possible to do that inside the loop?







      php laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 2:22









      JielJiel

      3410 bronze badges




      3410 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Your approach seems right, can you try (with namespace)



          $className = 'App' . $tables[$index];
          $class = new $className;


          So something like this in your example:



           $tables = [
          'AppCaves_demographic_info',
          'AppCaves_current_uses',
          'AppCaves_flora_outside',
          ];

          for($index = 0; $index < count($tables); $index++)
          $$tables[$index]."_model" = new $tables[$index];






          share|improve this answer

























          • still not working.. second approach produces same error. first approach produces unexpected error

            – Jiel
            Mar 26 at 2:27











          • Can you try it with namespace? I just tried in my code, with full namespace it should work

            – senty
            Mar 26 at 2:30












          • how would you parse the backslash in the string though?

            – Jiel
            Mar 26 at 2:35











          • As it is works for me, did you get an error?

            – senty
            Mar 26 at 2:35











          • I've got it. It worked. Thanks!

            – Jiel
            Mar 26 at 2:37











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348961%2fhow-to-instantiate-class-from-array-inside-the-loop%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your approach seems right, can you try (with namespace)



          $className = 'App' . $tables[$index];
          $class = new $className;


          So something like this in your example:



           $tables = [
          'AppCaves_demographic_info',
          'AppCaves_current_uses',
          'AppCaves_flora_outside',
          ];

          for($index = 0; $index < count($tables); $index++)
          $$tables[$index]."_model" = new $tables[$index];






          share|improve this answer

























          • still not working.. second approach produces same error. first approach produces unexpected error

            – Jiel
            Mar 26 at 2:27











          • Can you try it with namespace? I just tried in my code, with full namespace it should work

            – senty
            Mar 26 at 2:30












          • how would you parse the backslash in the string though?

            – Jiel
            Mar 26 at 2:35











          • As it is works for me, did you get an error?

            – senty
            Mar 26 at 2:35











          • I've got it. It worked. Thanks!

            – Jiel
            Mar 26 at 2:37
















          1














          Your approach seems right, can you try (with namespace)



          $className = 'App' . $tables[$index];
          $class = new $className;


          So something like this in your example:



           $tables = [
          'AppCaves_demographic_info',
          'AppCaves_current_uses',
          'AppCaves_flora_outside',
          ];

          for($index = 0; $index < count($tables); $index++)
          $$tables[$index]."_model" = new $tables[$index];






          share|improve this answer

























          • still not working.. second approach produces same error. first approach produces unexpected error

            – Jiel
            Mar 26 at 2:27











          • Can you try it with namespace? I just tried in my code, with full namespace it should work

            – senty
            Mar 26 at 2:30












          • how would you parse the backslash in the string though?

            – Jiel
            Mar 26 at 2:35











          • As it is works for me, did you get an error?

            – senty
            Mar 26 at 2:35











          • I've got it. It worked. Thanks!

            – Jiel
            Mar 26 at 2:37














          1












          1








          1







          Your approach seems right, can you try (with namespace)



          $className = 'App' . $tables[$index];
          $class = new $className;


          So something like this in your example:



           $tables = [
          'AppCaves_demographic_info',
          'AppCaves_current_uses',
          'AppCaves_flora_outside',
          ];

          for($index = 0; $index < count($tables); $index++)
          $$tables[$index]."_model" = new $tables[$index];






          share|improve this answer















          Your approach seems right, can you try (with namespace)



          $className = 'App' . $tables[$index];
          $class = new $className;


          So something like this in your example:



           $tables = [
          'AppCaves_demographic_info',
          'AppCaves_current_uses',
          'AppCaves_flora_outside',
          ];

          for($index = 0; $index < count($tables); $index++)
          $$tables[$index]."_model" = new $tables[$index];







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 at 2:34

























          answered Mar 26 at 2:25









          sentysenty

          4,2758 gold badges60 silver badges142 bronze badges




          4,2758 gold badges60 silver badges142 bronze badges












          • still not working.. second approach produces same error. first approach produces unexpected error

            – Jiel
            Mar 26 at 2:27











          • Can you try it with namespace? I just tried in my code, with full namespace it should work

            – senty
            Mar 26 at 2:30












          • how would you parse the backslash in the string though?

            – Jiel
            Mar 26 at 2:35











          • As it is works for me, did you get an error?

            – senty
            Mar 26 at 2:35











          • I've got it. It worked. Thanks!

            – Jiel
            Mar 26 at 2:37


















          • still not working.. second approach produces same error. first approach produces unexpected error

            – Jiel
            Mar 26 at 2:27











          • Can you try it with namespace? I just tried in my code, with full namespace it should work

            – senty
            Mar 26 at 2:30












          • how would you parse the backslash in the string though?

            – Jiel
            Mar 26 at 2:35











          • As it is works for me, did you get an error?

            – senty
            Mar 26 at 2:35











          • I've got it. It worked. Thanks!

            – Jiel
            Mar 26 at 2:37

















          still not working.. second approach produces same error. first approach produces unexpected error

          – Jiel
          Mar 26 at 2:27





          still not working.. second approach produces same error. first approach produces unexpected error

          – Jiel
          Mar 26 at 2:27













          Can you try it with namespace? I just tried in my code, with full namespace it should work

          – senty
          Mar 26 at 2:30






          Can you try it with namespace? I just tried in my code, with full namespace it should work

          – senty
          Mar 26 at 2:30














          how would you parse the backslash in the string though?

          – Jiel
          Mar 26 at 2:35





          how would you parse the backslash in the string though?

          – Jiel
          Mar 26 at 2:35













          As it is works for me, did you get an error?

          – senty
          Mar 26 at 2:35





          As it is works for me, did you get an error?

          – senty
          Mar 26 at 2:35













          I've got it. It worked. Thanks!

          – Jiel
          Mar 26 at 2:37






          I've got it. It worked. Thanks!

          – Jiel
          Mar 26 at 2:37









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348961%2fhow-to-instantiate-class-from-array-inside-the-loop%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript