Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Collection class in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the Collection class in PHP. It is very detailed and has a certain reference value. Friends who are interested must finish it!

I have been developing with. Net for many years, and recently I came into contact with php and found that php is also very interesting. However, it is found that there is no set Collection class, only an array, and the array is very strong. Here I use an array to wrap it into a collection Collection, with the following code:

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.

Let's first create a collection element class Course:

The copy code is 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, Chinese), 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 our daily development needs.

But we are now. There is an object delay loading in net. For example, if there is a Student object now, it should have a lot of Course, but we hope that Course will not load before accessing Course. That is to say, when instantiating Student, the number of Course is 0, and when we need Course, it actually reads the corresponding data from the database. It just requires us to instantiate Collection into inertia.

The modified Collection code is as follows:

The copy code is 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]))

}

/ * *

* Use this method to define a function to be

* 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 see 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 is as follows:

The copy code is 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, Chinese), 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)

The above is all the content of the article "how to use Collection classes in PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report