Creating instance of object from an array

Is it possible to store an array of strings in a database and create objects from them?

eg:-
     foreach (array("objOne", "objTwo", "objThree", "objFour") as $value) {) {
          $value = new myClass();
     }

Open in new window


My final idea is to store a list of required objects in a database, then go through them one by one and create the objects.
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim WalkerWeb Programmer/TechnicianCommented:
I guess that depends on what you want to do with those objects. If you want them to be global objects, you could do this:
     foreach (array("objOne", "objTwo", "objThree", "objFour") as $value) {) {
          $_GLOBALS[$value] = new myClass();
     }

Open in new window

Then they would be available as $objOne, $objTwo, etc.
0
Ray PaseurCommented:
Yes.  You might want to store them in the form of JSON strings.  But I am moved to ask a deeper question.  What is the business reason for this?  This is a very unusual design.  Databases with arrays embedded in columns almost certainly violate the normalization design principles that any DBA would advise, and they quickly become unwieldy.

If you can explain a little more about the underlying issues, maybe we can help you find a more rational approach.
0
tonelm54Author Commented:
What I'm thinking of doing is creating my website, then a folder for a load of modules. When the user logs onto the database it says which modules to load from the folder which then create new instance of the class which on the constructor downloads its settings from the database dependent on the user. Then when it gets to the point of drawing the html I can call $myClass->drawCSS then in the body $myClass->drawBody().

The idea is then when the user logs into the homepage it will create all the modules that user has specified and action it as required.

I thought this would be a good way of doing it, then I can easily allow one user to see lots of different things to another user, if anyone else has a better idea of how to do this Im open to suggestions.

Thank you
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Ray PaseurCommented:
Are "modules" the same as classes?
0
tonelm54Author Commented:
Ok, from doing a simple test:-
  class testMe
  {
      public $description = 'a default value';
      public function __construct() {
        }
      public static function test66() {
        echo "It works<br/>";
        }
  }
  
  foreach (array("objOne","objTwo","objThree","objFour") as $value) {
	$_GLOBAL[$value] = new testMe();
  }
  foreach (array("objOne","objTwo","objThree","objFour") as $value) {
	call_user_func($value . '->test66()');
  } 

Open in new window


I get "Warning: call_user_func() expects parameter 1 to be a valid callback, function 'objOne->test66()' not found or invalid function name"
0
Ray PaseurCommented:
Try it this way: http://www.laprbass.com/RAY_temp_tonelm54.php

<?php // RAY_temp_tonelm54.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

class testMe
{
    // THE CONSTUCTOR SAVES A VARIABLE FROM THE CLASS INSTANTIATION
    public function __construct($name) {
        $this->name = $name;
    }

    // THE METHOD TELLS THE CLASS NAME AND THE SAVED VARIABLE
    public function test66() {
        echo $this->name
        . ' IS AN OBJECT INSTANCE OF '
        . __CLASS__
        . '<br/>'
        ;
    }
}

// HERE ARE THE OBJECT NAMES
$arr = array
( "objOne"
, "objTwo"
, "objThree"
, "objFour"
)
;

// INSTANTIATE THE OBJECT INSTANCES OF THE SAME CLASS
foreach ($arr as $value) {
	$objects[$value] = new testMe($value);
}

// SHOW THE INTERMEDIATE DATA
print_r($objects);

// CALL THE METHOD ON EACH OF THE OBJECT
foreach ($objects as $obj) {
	$obj->test66();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
greetings tonelm54, , not to sure you have a good idea about this set up for a PHP page, you say -

"When the user logs onto the database it says which modules to load from the folder which then create new instance of the class which on the constructor downloads its settings from the database dependent on the user."

You might be better to save the "name" of the Class (module) rather than the name of the "Variable" that accesses the instance of the class, the name of the variable is totally irrelevant for what you say you need to do, such as -
$value = new myClass();
if it is different Classes that you need to use, If you use this sort of thinking-coding -
objArray[$value] = new myClass();
then I do not see a practical way to have all of the following CODE in your PHP to successfully call your -
$myClass->drawBody();  as-
objArray["UnKnown"]->drawBody();
as the "UnKnown" would be different for different Variable names from a DB load. Is it that you use different amount of variables, or different variable names in the different pages? Or do use use different Classes (modules) in the different pages?

Not really sure what you need to do, I would think that you will have to have some module set up code like -
$moduleA = new aMVC($user);
$moduleB = new bMVC($user);
// other code
$moduleA->drawBody();
$moduleB->drawBody();

So how do these Class module instances  like -
$moduleA = new aMVC($user);
change from page to page?
0
Ray PaseurCommented:
@Slick812: You don't ever need to save the name of the class.  PHP keeps track of that for you.
http://php.net/manual/en/language.constants.predefined.php
0
Slick812Commented:
@Ray_Paseur, sorry , I was to quick  in my explanation.
I scribbled - ""You might be better to save the "name" of the Class (module) rather than the name of the "Variable" "
What I meant was not a regular save, but some form of SAVE to the data base, although I said "name" of the Class", ,  I should have said a reference or something to tell the Coder which Class was needed to load for that particular page

For instance -
I have done pages where the users After they sign-in are Identified with a standing or status in their access to stuff in the CMS or MVC Class-Object used for HTML output for their individual pages

After the user signs in and the SELECT for the database returns a column called "status" which can have 4 values as "free" , "paid" , "mod" and "admin"
each of these have Different Classes loaded for different HTML output sections of the page as their "Access"  to restricted content, Here the MCV MENU Class will be done as -
// After sign and a SELECT for the user in DB the important data like user-name and status are saved in the SESSION

// The SELECT returns a $row with the "status" column in it
// do a SWITCH to load different Classes
switch ( $row["status"] ){
    case "paid" : 
        setPaided(1); // This function SETS all of the Page Values necessary for the Paid user
        $menu = new mcvMenuPaid($user); // A different function and menu CLASS is loaded for each different status
        break;
    case "mod" : 
        setMod(2);
        $menu = new mcvMenuMod($user);
        break;
    case "admin" : 
        setAdmin(3);
        $menu = new mcvMenuAdmin($user);
        break;
    default: 
        setFree(0);
        $menu = new mcvMenuFree($user);
    } // end of switch

//Later in the code
<div class="dMenu">
   <?php $menu->drawMenu($theme); ?>
</div>

Open in new window

Maybe this will help some, , but I am guessing at what tonelm54 needs to do, if tonelm54 could give some code examples of how the code was different from one page to another, so we could see what needs to be done to have a database storage for the necessary information. But just changing the Variable names from a database store, can not change how the page works (output), I would not think? ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.