2

I'm new to php. I just want to create an object from array of strings.

I want these array of strings:

$arrEmails = [
   '[email protected]',
   '[email protected]',
   '[email protected]'
];

to be put in an object to be accessed like, $emails = $objEmails->email

How is it possible?

2
  • just create an empty object, then declare the property, and why do you need such fashion anyway? Commented Apr 11, 2015 at 10:13
  • can you provide a sample code? Commented Apr 11, 2015 at 10:14

3 Answers 3

1

Just create your empty object and just simply add that property:

$objEmails = new stdClass; // initialize an empty object
$objEmails->email = $arrEmails; // declare the property
Sign up to request clarification or add additional context in comments.

Comments

1

You should have a class like this:

class ObjEmails
{
    $emails = array();
    public function __construct()
    {
        $this->emails = array(
          '[email protected]',
          '[email protected]',
          '[email protected]'
        );
    }

}

$objEmails = new ObjEmails;
$emails = $objEmails->email;

1 Comment

Thanks for this answer but I chose the simplest.
0

You can also try with -

$arrEmails = [
   '[email protected]',
   '[email protected]',
   '[email protected]'
];
$objEmails = (object) $arrEmail;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.