Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare 'sub-objects' in PHP

Tags:

oop

php

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. I'd appreciate any pointers to tutorials or documents which might help - I'm not expecting a full-blown answer here.

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 this:

$mylibrary->sublibraries[0]->element[0]

to retrieve a specific element?

As I say, I don't expect a full-blown explanation here - just pointers to get me started.

like image 890
Sharon Avatar asked Oct 19 '11 18:10

Sharon


People also ask

How do you declare an object variable in PHP?

The object is declared to use the properties of a class. The object variable is declared by using the new keyword followed by the class name.

What is a stdClass object?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

How do you call an array of objects in PHP?

Array elements can be accessed using the array[key] syntax. ); var_dump($array["foo"]); var_dump($array[42]);

What are the different objects in PHP?

Primary (scalar) variables, arrays and other objects can be cast to object data type using casting operator. PHP provides stdClass as a generic empty class which is useful for adding properties dynamically and casting.


1 Answers

<?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]

?>
like image 155
Peter Avatar answered Oct 03 '22 06:10

Peter