Page 1 of 1

Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 1:55 pm
by registereduser
I really need your help because I could not figure out what is wrong with my codes. What I need is to turn select result into two arrays. The first array $baseCateList is to record the select result. The second array $baseLinkList's select criteria is based on the first array. So, for this reason the 2nd array is a 3 dimensional array.

I do not know why my code does not work. I guess either something wrong with the __construct or something with complete idea. I tried to search Internet and never be able to find an example of 2d and 3d array object.

Code: Select all

 
<?php
  class Linkitem
  { private $user_id; 
 
    private $baseCateList;  // This 2 dimensional array.
    private $baseLinkList;  // This 3 dimensional array.
 
    public function __construct($u=null)
    { $this->reconstruction($u);
    }
 
    public static function reconstruction($u)
    { $link = new Linkitem();
      $link->user_id = $u;
      $link->assignBaseCateList($link->lang_id);
      $link->assignBaseLinkList($link->baseCateList);
      return $link;
    }
 
    public static function assignBaseCateList($uid)
    { $query = sprintf( ' SELECT ID_CATEBASE     ' .                        
                        ' , TITLE_1              ' .
                        ' FROM %sCATES_BASE      ' .
                        ' WHERE ID_LANGUAGE = %d ' .
                      , DB_TBL_PREFIX
                      , $uid
                      );
      $result = mysql_query($query, $GLOBALS['DB']);
      while($row = mysql_fetch_array($result))
      { $baseCateList[] = array( "id_catebase" => $row['ID_CATEBASE']
                               , "title_1"     => $row['TITLE_1']
                               );
      }; 
      mysql_free_result($result);
    }
 
    public static function assignBaseLinkList($baseCateList)           
    { for ($i=0; $i<count($baseCateList); $i++)
      { $baseLinkList[$i] = $this->getEachBaseLinkList($baseCateList['id_catebase']);
      };
    }
 
    public static function getEachBaseLinkList($baseCate_id)           
    { $eachBaseLinkList = Array();
      $query = sprintf( ' SELECT URL             ' .                        
                        ' , TITLE                ' .
                        ' FROM %sLINKS_BASE      ' .
                        ' WHERE ID_CATEBASE = %d ' .
                      , DB_TBL_PREFIX
                      , $baseCate_id
                      );
      $result = mysql_query($query, $GLOBALS['DB']);
      while($row = mysql_fetch_array($result))
      { $eachBaseLinkList[] = array( "url"      => $row['URL']
                                   , "title"    => $row['TITLE']
                                   );
      }; 
      mysql_free_result($result);
      return $eachBaseLinkList;
    }
  } // End of class.
?>
 

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 2:29 pm
by McInfo
1. The first thing to do is remove the two extra dots found at the ends of lines 25 and 49.

2. Infinite recursion crashes the server.

Code: Select all

<?php
$li = new Linkitem();
class Linkitem {
    public function __construct($u=null) {
        $this->reconstruction($u);
    }
    public static function reconstruction($u) {
        $link = new Linkitem();
        return $link;
    }
}
Edit: This post was recovered from search engine cache.

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 2:59 pm
by registereduser
Thanks a lot. I indeed have shortened the code, such as other SELECT and ORDER BY clauses. That is where two extac . comes from.

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 3:07 pm
by registereduser
Thanks a lot for 2nd point. I changed the code. Would it work?

Code: Select all

<?php
$li = new Linkitem();
class Linkitem {
    public function __construct($u=null) {
        $this->reconstruction($u);
    }
 
    public static function getNewObj($u) {
        $link = new Linkitem();
        $link->reconstruction($u);
        return $link;
    )
 
     public static function reconstruction($u) {
         // Something here.
     }
}

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 3:10 pm
by McInfo
It looks like it should work. Try it.

Edit: It won't work because you can't access static methods with the -> syntax.

Edit: This post was recovered from search engine cache.

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 3:26 pm
by registereduser
No, it does not work:
Fatal error: Using $this when not in object context in php/linkitem.php on line xx

The error was in reconstruction($u), because I used $this->. I do not understand why I could not use $this-> here. Any idea?

Code: Select all

<?php
$li = new Linkitem();
class Linkitem {
    public function __construct($u=null) {
        $this->reconstruction($u);
    }
 
    public static function getNewObj($u) {
        $link = new Linkitem();
        $link->reconstruction($u);
        return $link;
    )
 
     public static function reconstruction($u) {
         // Something here.
     }
}

Re: Object for 3 Dimensional Array

Posted: Mon Nov 16, 2009 3:46 pm
by McInfo
Sun's Java documentation has a well-written explanation of Object-Oriented Programming Concepts. Even though it's targeted at Java, the concepts still apply.

Static fields and methods (variables and functions) belong to the class. Non-static fields and methods belong to instances of the class (objects). When a static field is changed by one instance, that field changes for every instance of the same class. Non-static fields change for only one instance.

The documentation can hopefully explain things better than I can.

PHP Manual: Classes and Objects

Edit: This post was recovered from search engine cache.

Re: Object for 3 Dimensional Array

Posted: Tue Nov 17, 2009 7:45 am
by registereduser
Hi McInfo, thank you very much for help. I have identified where the problem is, though I could not fixed it. The problem is I can write into the arrays, but could not get the data out. Every time I call it by use $obj->something method, if the 'something' the single variable, I can get it. If it is a single dimensional array, I can get it too. However, if it is the 2 or more dimensional array, I could not get it.

I think it is because __get('key') is called automatically, and default __get() only having key=>value pair structure, which does only work for single dimensional array. So, what I need now is get an overrided __get() in structure of __get('key1', 'key') or __get('key1.key2.key3'). I really need help with this part.

I tried change from private to public, which does not work either. It is no longer an array, it is object now.

Re: Object for 3 Dimensional Array

Posted: Tue Nov 17, 2009 4:37 pm
by McInfo
The __set() and __get() magic methods are called implicitly. They are not meant to be called directly like other methods. They make getting and setting variables more convenient.

Code: Select all

// Setting
$obj->var = $val;
// Same as
$obj->__set('var', $val);
 
// Getting
$val = $obj->var;
// Same as
$val = $obj->__get('var');
It doesn't matter what $val is. It could be a number, string, object, array, multi-dimensional array, etc.

Are you trying to get values using chaining?

Code: Select all

$val = $obj->var->child;
I'm not clear on what your goal is. Please explain.

Edit: This post was recovered from search engine cache.

Re: Object for 3 Dimensional Array

Posted: Wed Nov 18, 2009 3:03 pm
by registereduser
Hi McInfo. I found the solution. Within function, I declare the new variable, then assign the value to the variable. The value comes from the database. Then return that variable:

public static function funcationToRetrieveDB()
{ $localVar = [something from db];
return $localVar;
}

In constructor, I add:

$this->classVariable = $this->funcationToRetrieveDB();

I got what I need. I was not aware that, even in function within the class, it is not allowed to directly access the class variable. What I tried but failed was:

public static function funcationToRetrieveDB()
{ $this->classVariable = [something from db];
}

Thanks a lot, McInfo. Can anyone tell me is my failed attempt allowed in Java?

Re: Object for 3 Dimensional Array

Posted: Thu Nov 19, 2009 11:48 am
by McInfo
Here is an analogy involving a press conference. The person at the microphone speaks and all of the reporters listen and take notes.

The speaker's presentation is like a static variable. The presentation is the same to all reporters.

The act of a reporter listening to the presentation is like a static function. All reporters do it and they all do it at the same time. No reporters are in control of listening, it is done for them.

The reporters' notes are like non-static variables. Every reporter has them, but they are different for each reporter. The speaker does not have access to any reporter's notes. Reporters do not have access to each others' notes except when two reporters have a conversation.

The act of a reporter asking a question is like a non-static function that influences a static variable. Every reporter can do it, but each reporter does it independently. Asking a question can influence the speaker's presentation.

The act of a reporter writing in a notebook is like a non-static function that influences a non-static variable. Every reporter can do it, but each reporter does it independently. What a reporter writes can be seen only by the reporter who wrote it.

Code: Select all

<?php
class Reporter {
 
    // Static variables are shared by all objects of the class
    static $presentation = '';
 
    // Non-static variables are not shared
    var $name = '';
    var $notes = array();
 
    function __construct ($name) {
        $this->name = $name;
    }
 
    // Static functions can use static variables
    static function listen ($words) {
        self::$presentation .= $words.' ';
    }
 
    // Non-static functions can use non-static variables
    function takeNote ($note) {
        $this->notes[] = $note;
    }
    function readNotes () {
        return $this->notes;
    }
 
    // Non-static functions can use static functions
    function askQuestion ($question) {
        self::listen('['.$this->name.'] '.$question);
        return '[Speaker] I dunno.';
    }
 
    // Non-static functions can use static variables
    function playPresentation () {
        return self::$presentation;
    }
}
 
// Enter two reporters:
$sue = new Reporter('Sue');
$joe = new Reporter('Joe');
 
// A long presentation ensues...
$joe->takeNote('I wonder when this will be over.');
 
// The presentation nears an end.
Reporter::listen('[Speaker] Does anyone have a question?');
$sue->takeNote('Begin Q&A.');
Reporter::listen($joe->askQuestion('When is lunch?'));
$sue->takeNote('Joe is a dork.');
 
// Back at Sue's office:
echo $sue->playPresentation();
echo "\nSue's notes: ";
print_r($sue->readNotes());
 
// Back at Joe's office:
echo $joe->playPresentation();
echo "\nJoe's notes: ";
print_r($joe->readNotes());
 
// Sue and Joe have the same recording of the presentation but different notes.
Edit: Replaced example code with something more topical.

Edit: This post was recovered from search engine cache.

Re: Object for 3 Dimensional Array

Posted: Thu Nov 26, 2009 4:33 pm
by registereduser
Thanks McInfo. I have read so many times of your analogue and still could not fully understand. Anyway, I think I got some ideas about class method and object method.