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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题