A brief analysis of the design _php skills of collection class in PHP

Source: Internet
Author: User
Developed with. NET for many years, recently contacted PHP, and found PHP is also very fun. However, it is found that there is no set collection class, only arrays, and the array is very strong. Here I wrap the array into a set collection, and the code is as follows:
Copy Code code as follows:

Class collection{
Private $_members=array ();

Public Function AddItem ($obj, $key =null)
{
if ($key)
{
if (Isset ($this->_members[$key]))
{
Throw new Exception ("Key \" $key \ "already in use!");
}
Else
{
$this->_members[$key]= $obj;
}
}
Else
{
$this->_members[]= $obj;
}
}

Public Function RemoveItem ($key)
{
if (Isset ($this->_members[$key]))
{
unset ($this->_members[$key]);
}
Else
{
Throw new Exception ("Invalid Key \" $key \ "!");
}
}
Public Function GetItem ($key)
{
if (Isset ($this->_members[$key]))
{
return $this->_members[$key];
}
Else
{
Throw new Exception ("Invalid Key \" $key \ "!");
}
}

Public Function Keys ()
{
Return Array_keys ($this->_members);
}

Public Function legth ()
{
return sizeof ($this->_members);
}

Public function exists ($key)
{
Return (Isset ($this->_members[$key]));
}
}

Now let's test whether this collection works.
We first set up a collection element class course:
Copy Code code as follows:

Class Course
{
Private $_id;
Private $_coursecode;
Private $_name;

Public function __construct ($id, $courseCode, $name)
{
$this->_id= $id;
$this->_coursecode= $courseCode;
$this->_name= $name;
}

Public Function GetName ()
{
return $this->_name;
}

Public Function GetID ()
{
return $this->_id;
}

Public Function Getcoursecode ()
{
return $this->_coursecode;
}

Public Function __tostring ()
{
return $this->_name;
}
}

The test code is as follows:
$courses =new Collection ();
$courses->additem (New Course (1, "001", "Language"), 1);
$courses->additem (New Course (2, "002", "Mathematics"), 2);
$obj = $courses->getitem (1);
Print $obj;
I think this collection class should be able to meet the requirements of our weekday development.
But we are. NET has an object delay loading, for example, if there is student this object, it should have a lot of course, but we want to visit the course before course is not loaded. That is to say, when the student is instantiated, the number of course is 0, and when we need course, it actually reads the data from the database. Is that we need to make collection into an inert instantiation.
The modified collection code is as follows:
Copy Code code as follows:

Class Collection {
Private $_members = Array (); Collection Members
Private $_onload; Holder for callback function
Private $_isloaded = false; Flag that indicates whether the callback
has been invoked
Public Function AddItem ($obj, $key = null) {
$this->_checkcallback (); _checkcallback is defined a little later

if ($key) {
if (Isset ($this->_members[$key])) {
throw new Keyinuseexception ("Key \" $key \ "already in use!");
} else {
$this->_members[$key] = $obj;
}
} else {
$this->_members[] = $obj;
}
}
Public Function RemoveItem ($key) {
$this->_checkcallback ();

if (Isset ($this->_members[$key])) {
unset ($this->_members[$key]);
} else {
throw new Keyinvalidexception ("Invalid key \" $key \ "!");
}
}

Public Function GetItem ($key) {
$this->_checkcallback ();

if (Isset ($this->_members[$key])) {
return $this->_members[$key];
} else {
throw new Keyinvalidexception ("Invalid key \" $key \ "!");
}
}
Public Function keys () {
$this->_checkcallback ();
Return Array_keys ($this->_members);
}
Public function Length () {
$this->_checkcallback ();
return sizeof ($this->_members);
}
Public function exists ($key) {
$this->_checkcallback ();
Return (Isset ($this->_members[$key]));
}
/**
Define a function to IS
* invoked prior to accessing the collection.
* The function should take a collection as a
* its sole parameter.
*/
Public Function Setloadcallback ($functionName, $objOrClass = null) {
if ($objOrClass) {
$callback = Array ($objOrClass, $functionName);
} else {
$callback = $functionName;
}

Make sure the Function/method is valid
if (!is_callable ($callback, False, $callableName)) {
throw new Exception ("$callableName is not callable".
"As a parameter to onload");
return false;
}

$this->_onload = $callback;
}

/**
* Check to the If a callback has been defined and if so,
* Whether or not it has already been called. If not,
* Invoke the callback function.
*/
Private Function _checkcallback () {
if (Isset ($this->_onload) &&! $this->_isloaded) {
$this->_isloaded = true;
Call_user_func ($this->_onload, $this);
}
}
}

The required student are as follows:
Copy Code code as follows:

Class Coursecollection extends Collection {
Public Function AddItem (Course $obj, $key =null) {
Parent::additem ($obj, $key);
}
}
Class student{
Private $_id;
Private $_name;
Public $course;

Public function __construct ($id, $name)
{
$this->_id= $id;
$this->_name= $name;
$this->course=new coursecollection ();
$this->course->setloadcallback (' loadcourses ', $this);
}

Public Function GetName ()
{
return $this->_name;
}

Public Function GetID ()
{
return $this->_id;
}

Public Function __tostring ()
{
return $this->_name;
}
Public Function loadcourses (Collection $col)
{
$col->additem (New Course (1, "001", "Language"), 1);
$col->additem (New Course (2, "002", "Mathematics"), 2);
}
}

The calling code is as follows:
$student =new Student (1, "Majiang");
Print $student->getname ();
Print $student->course->getitem (1);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.