Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3627476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:53:36+00:00 2026-05-18T23:53:36+00:00

I’m trying to cast an array of Objects for a week already in a

  • 0

I’m trying to cast an array of Objects for a week already in a PHP class and have had some real issues making it to work and mainly with the logic itself as I am new to classes. Looked and read a lot of resources but it does not seem to make any sense to me, any pointers would be greatly appreciated and I am open to suggestions.

The issue:
Create a PHP class as part of the project named Contact, and then a class called ‘ContactList’ that contains an array of these contact objects.

And next, an array of ContactList objects called ‘ContactTabs’.

Then, in the program, populate one ContactList object (named ‘Contacts’) with the current contacts, and create a new ContactList object named ‘Friends’, and add some names and email addresses there for friends. It is most important that this be done in a nice, object-oriented fashion so it can allow to create other type of contacts in the future.

A ‘ContactList’ object, should contain not only an array that is the list of contacts, but it would also contain the text label to put on the tab. So, it is more appropriate that the ContactList be more than a simple array, but rather it should be an object that contains an array as well as a text label.

The business logic is the following:

Contact
name
bgcolor
lgcolor
email

ContactTabs
Employees
Friends

     // class definition 
class Contact{ 
    // define properties 
    public $name; 
    public $bgcolor; 
    public $lgcolor; 
    public $email; 

    // constructor 
   public function __construct() { 

    } 

  //destructor 
  public function __destruct() { 

   } 

}

class ContactList extends Contact { 


    // constructor 
    public function __construct($contactname,$contactbgcolor,$contactlgcolor,$contactemail) {

 $this ->name = $contactname; 
 $this ->bgcolor = $contactbgcolor; 
 $this ->lgcolor = $contactlgcolor;
 $this ->email = $contactemail;  

    parent::__construct();
  }

  }

$johndie = new ContactList('John Die','#FCEDC9','#FEF9ED','somecontact1@gmail.com','9');

$johndoe = new ContactList('John Doe ','#DEEDFE','#EDF5FE','somecontact2@hotmail.com,'6');


$Friends = new ExtendedArrayObject($jp);
$Employees = new ExtendedArrayObject($elvete);

$ContactTabs= new ExtendedArrayObject($Employees,$Friends);

print_r($ContactTabs);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:53:37+00:00Added an answer on May 18, 2026 at 11:53 pm

    You had the Contact class correct (although you may want to use private/protected properties for encapsulation, but you can change that later).

    This is how I would do it:

    class Contact{ 
        public $name; 
        public $bgcolor; 
        public $lgcolor; 
        public $email;
    
        public function __construct($name, $bgcolor, $lgcolor, $email) {
            $this->name = $name;
            $this->bgcolor = $bgcolor;
            $this->lgcolor = $lgcolor;
            $this->email = $email;
        }
    }
    
    class ContactList implements Iterator, ArrayAccess {
        protected $_label;
        protected $_contacts = array();
    
        public function __construct($label) {
            $this->_label = $label;
        }
    
        public function getLabel() {
            return $this->label;
        }
    
        public function addContact(Contact $contact) {
            $this->_contacts[] = $contact;
        }
    
        public function current() {
            return current($this->_contacts);
        }
    
        public function key() {
            return key($this->_contacts);
        }
    
        public function next() {
            return next($this->_contacts);
        }
    
        public function rewind() {
            return reset($this->_contacts);
        }
    
        public function valid() {
            return current($this->_contacts);
        }
    
        public function offsetGet($offset) {
            return $this->_contacts[$offset];
        }
    
        public function offsetSet($offset, $data) {
            if (!$data instanceof Contact)
                throw new InvalidArgumentException('Only Contact objects allowed in a ContactList');
    
            if ($offset == '') {
                $this->_contacts[] = $data;
            } else {
                $this->_contacts[$offset] = $data;
            }
        }
    
        public function offsetUnset($offset) {
            unset($this->_contacts[$offset]);
        }
    
        public function offsetExists($offset) {
            return isset($this->_contacts[$offset]);
        }
    }
    

    And ContactTabs would be very similar to ContactList, but would accept ContactList objects instead of Contact.

    How it would work is:

    // create some contacts
    $bob = new Contact('Bob', 'black', 'white', 'bob@bob.com');
    $john = new Contact('John', 'black', 'white', 'john@john.com');
    
    // create a contact list and add contacts to it
    $contactlist = new ContactList('Contacts');
    $contactlist->addContact($bob);  // using a method
    $contactlist[] = $john; // using array notation
    
    // access the list by using foreach on it, since ContactList implements Iterator
    foreach ($contactlist as $contact) {
         echo $contact->email;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.