We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2912
    • 315 Posts
    Hi all,

    I know this is probably not the best place to post this message and although I have searched I have been unsuccessful in finding an answer.

    I’ve been coding a small program and using modX as my CMS core. My problem is how to create a class that use to other files containing classes and reference them accordingly.

    For instance, my program contains the following files:

    ncr.php
    data.php
    file.php

    my main class is called through a snippet:

    $complaint = new NCR();
    
    return $complaint->Run();
    


    I then make a connection to the other two files using require_once and $this->db and $this->fl.

    My problem is when adding functions inside the data class I dont seem to be able to call functions contained within the file class.

    I find my code works but is rather embarassing to look over. Am I missing something fundamental in trying to call a function in this manor?

    I’m thinking I should be able to call it from the main ncr class downward but alas it just aint so.

    I’m lost if anyone could help I would really appreciate it.

    Thanks,

    BBloke
      BBloke
      • 22303 MODX Staff
      • 10,725 Posts
      How are you instantiating $complaint->db and $complaint->fl ? If you can include more of the code you’ve used to establish these references, perhaps we can provide some advice.
        • 2912
        • 315 Posts
        Thanks OpenGeek,

        In the main ncr class I have:

        	var $db;
        	var $fl;
        
        function NCR() {
        		require_once "data.php";
        		require_once "file.php";
        		
        		$this->db = new dataDB;
        		$this->fl = new flData($this->db->dbTable, $this->db->config);
        }
          BBloke
          • 22303 MODX Staff
          • 10,725 Posts
          Ok, I think the problem here is trying to use $this in the constructor when instantiating the file class. Try creating a local var in the constructor to represent the $db var since $this is really not valid as an accessor until after the constructor has completed:
          $db = new dataDB();
          $fl = new flData($db->dbTable, $db->config);
          $this->db = $db;
          $this->fl = $fl;
          
            • 2912
            • 315 Posts
            Thanks for another quick reply. I’m thinking I’ve either described it wrong or I’m not just getting this at all.

            I can make it all work great if I create another object in the data class to the file class but I don’t want to do that. At the moment, to me it looks like:

               ncr
              / \
            file \
                  data
            


            I can call any function from either file or data whilst in the ncr class but I can’t call a function from data that is contained within file unless I do the following:

            class data() {
            				
            		$this->fl = new flData($this->db->dbTable, $this->db->config);
            }
            
            I'm stumped but have it working to some degree although I think its about as inefficient as it can be.
            
            Thanks Open Geek.  I'm planning on getting some books in to help with this.
              BBloke