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 6703901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:35+00:00 2026-05-26T07:13:35+00:00

I’m relatively new to OOP in PHP, and I’m not sure if what I’m

  • 0

I’m relatively new to OOP in PHP, and I’m not sure if what I’m trying to do is possible or recommended. In any case, I can’t figure it out. Are there any tutorials or documentation which might help?

I have a system in which each user has a number of ‘Libraries’. Each Library contains a number of ‘Elements’.

DB set up is as follows:

user_libraries
 - id (unique)
 - user_id (identifies user)
 - name (just a string)

elements
 - id (unique)
 - content (a string)

library_elements
 - id (unique)
 - library_id
 - element_id

where library_id is the id from user_libraries, and element_id is that from elements.

I want to be able to access a given user’s library, and their elements.

I’ve set up the library class, and can use it to retrieve the list of libraries (or a sub-list).

I do this like this:

$mylibraryset = new LibrarySet();
$mylibraryset->getMyLibraries();

which gives (when I use print_r):

LibrarySetObject (
 [user_id] => 105
 [data_array] => Array (
  [0] => Array (
   [id] => 1
   [user_id] => 105
   [type] => 1
   [name] => My Text Library
  )
  [1] => Array (
   [id] => 2
   [user_id] => 105
   [type] => 2
   [name] => Quotes
  )
 )
)

Now, what I’d like to be able to do is for each of those libraries (the elements in data_array), to retrieve all the elements.

The best idea I’ve had so far is to do something like:

foreach($mylibrary->data_array as $library) {
 $sublibrary = new Library();
 $sublibrary -> getAllElements();
}

where Sublibrary is another class which has the function getAllElements. I can’t quite get it to work though, and I’m not sure I’m on the right lines…

Is there a way that I can then end up being able to do something like the following, to retrieve a specific element?

$mylibrary->sublibraries[0]->element[0]
  • 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-26T07:13:35+00:00Added an answer on May 26, 2026 at 7:13 am
    <?php
    
    class Library {
        public $element;
        public $data;
        public function __construct($sublibrary) {
            $this->data = $sublibrary;
        }
        public function getAllElements() {
            // populate $this->element using $this->data
        }
    }
    
    class LibrarySet {
        public $user_id;
        public $data_array;
        public $sublibraries;
        public function getMyLibraries() {
            // populate $this->data_array
    
            $this->sublibraries = Array();
            foreach($this->data_array as $index => $sublibrary) {
                $this->sublibraries[$index] = new Library($sublibrary);
                $this->sublibraries[$index]->getAllElements();
            }
        }
    }
    
    $mylibraryset = new LibrarySet();
    $mylibraryset->getMyLibraries();
    
    $mylibraryset->sublibraries[0]->element[0]
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.