doujiu8826 2010-12-24 13:02
浏览 42
已采纳

如何投射一个对象数组

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);
  • 写回答

2条回答 默认 最新

  • duanmeng3126 2010-12-24 14:09
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器