Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Help with array in classes

    I'm learning OOP techniques, and I've run into a problem I ca't figure out.

    What I'm trying to do is call an array from one class in another.

    Here's what I'm trying to do

    PHP Code:
    class calssOne {

        public function 
    functionOne(){
             
    $myArray = array(
                array( 
    'std' => '' 'type' => 'checkbox' 'pre' => 'prefix_' 'name' => 'theName' )
            );
            
            foreach(
    $myArray as $value){
                
    $foreachArray = array( $value['pre'],$value['name'] => $value['std']);
            }
        }
    }

    class 
    classTwo {
        public function 
    functionTwo() {
        
            
    $get_class = new calssOne();
            
    $get_class -> functionNameOne();
                
            if (isset(
    $_POST['update_options'])) { 

                foreach(
    $myArray as $value){
                    if (isset(
    $_POST[$value['name']])) { 
                        
    $myOptions[$value['pre'],$value['name']] = apply_filters($value['pre'],$value['name'].'_save_pre'$_POST[$value['name']]);
                    }

                } 
            }
        }

    But I just can't get the array to work with functionTwo(). I've tried to make $myArray global and return it, but it keeps throwing errors.

    Any Ideas?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You never will. You need to return a result for the $myArray from functionOne or store it in a public member which can be accessed by classTwo (not recommended). Globalizing will work, but it takes more work to do, and also not recommended. An alternative is to provide a referenced array variable to functionOne which can write the necessary data into it. That variable would be declared by functionTwo and passed as an argument.

  • Users who have thanked Fou-Lu for this post:

    gilgimech (11-08-2011)

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    If you intend to use classes then my suggestion would be to stick with the philosophy behind OOP and if you want to use data from object B in object A then you need to provide a function in object B that returns that data and object A then creates an instance of object B and calls the function that returns that data

    PHP Code:
     
    class ClassA {
      
       
    $classB = new ClassB();
     
       
    $classBVariable $this->classB->getVariable();
    }
     
    class 
    ClassB {
     
       private 
    $classMemberVariable;
     
       public function 
    getVariable() {
     
          return 
    $classMemberVariable;
     
       }
     

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    gilgimech (11-08-2011)

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Spookster View Post
    If you intend to use classes then my suggestion would be to stick with the philosophy behind OOP and if you want to use data from object B in object A then you need to provide a function in object B that returns that data and object A then creates an instance of object B and calls the function that returns that data
    OK, so what you're saying is that I was doing it backwards. Instead of calling the array in the second class I should have the first class call the second class function.

    Thanks but that won't work for what I'm working on. I'm starting to think OOP isn't all it cracked up to be. To many restrictions.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    OOP is above and beyond what its cracked up to be. The problem is that its simply a major paradigm shift from that of procedural, and this requires a major change in the thought process for how to write the code.
    Traditionally you provide accessor and mutator methods for each of the members within a class that you want to allow access to. In a language like PHP, this tradition becomes necessity due to its datatype weakness. When it comes to asking for information, you must always ask the object for it, otherwise there is no context in which information can be gathered. This context is what really dictates the difference between the OOP and procedural code. You can ask a function what color it is (assuming it simply returns a color), but you cannot ask a class what color it is (assuming its color is a non-static property). You can ask any instance of that class what its color is though. The class itself is simply a template, if a Car class exists with a make, model and color, all we know about Car itself is that it has a make, model, and color (accessed via methods, not members - this becomes more apparent in its usefulness when you learn interfacing).

    I can't provide any recommendations for what you are doing since it is unclear. The only thing that is clear is that a composite relationship exists between classTwo and calssOne, but its not clear if these methods require a static or non-static context (from what you have they are non-static, but I don't know if that is the intent).

  • Users who have thanked Fou-Lu for this post:

    gilgimech (11-08-2011)

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Yeah, it's just frustrating. From what I understand you have a class and then you can use other classes to expand on the original class.

    I must not understand it then.

    I basically just want to organize my code with OOP. Instead of one long 1000 line file. Have multiple files with classes that work together. I can do that without classes just fine. Just using functions and global variables, but it just seem that OOP is a better route to take.

    You see the classes above are actually from separate files. I was thinking I could just create one class on a file, and then just include other files (writen in procedural code) into that one class. But that somehow feels like cheating.

    What do you think?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No this is the best way to do it; each class to a file. Only load what you need for dependency instead of everything. Plus with 5.3 and namespaces, you can override the __autoload and set the destination directory for all class files. This chains the 'use' keyword to the __autoload, so that's super handy.
    You can combine OO and procedural together. No matter what, in PHP you require at least one procedural statement, even if its a simple new MyClass(); since there is no entrance point to classes themselves in PHP.
    As for a conversion, that does take a lot of work. Its an entire rewrite of procedural code, you cannot just split them up. Generally speaking, anything done in OO code will take more code and work to write and process than it will in procedural code.

  • Users who have thanked Fou-Lu for this post:

    gilgimech (11-08-2011)

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Fou-Lu thanks for you help. I looked into referencing a array variable. I think it's got on the right track now. Thanks again.

  • #9
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    OK, so I've been banging my head over this for a while now. I still can't get this to work.

    Let's say I have two classes Class1 and Class2

    Class1 has an array and I want to use that array in Class2. From what I figure it should be done like this.
    PHP Code:
    class Class1 
            
        var 
    $the_array = array(); 
            
        function 
    __construct() {
            
    $this->the_array = array('1','2','3' );
        }
            
        function 
    get_the_array() {
            return 
    $this->the_array;
        }
            
    }

    class 
    Class2
            
        function 
    display_the_array(){ 
            
        
    $get_Class1 = new Class1();
        
    $get_array $get_Class1->get_the_array();
                
        echo 
    $get_array ;
                
        }
        

    But I'm still getting an empty array. Can Anyone help me with this?
    Last edited by gilgimech; 11-23-2011 at 06:04 PM.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have no variable called $get_class1. Variables are case sensitive.
    You also cannot echo a variable, you need to print_r it or var_dump it, or iterate it and print each result. Also, drop the use of 'var' keyword, its deprecated and exists only for the purpose of backward compatibility with PHP4.x. Use private instead.

    Edit:
    Sorry I meant you cannot echo an array, not a variable.
    Last edited by Fou-Lu; 11-23-2011 at 05:55 PM.

  • Users who have thanked Fou-Lu for this post:

    gilgimech (11-23-2011)

  • #11
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    You have no variable called $get_class1.
    Typo I'll fix it. It should have been &get_Class1.

    Ok, can't echo array's. Good to know.

    Other than that should this work?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    It will when you invoke the class2::display_the_array method.

  • Users who have thanked Fou-Lu for this post:

    gilgimech (11-23-2011)

  • #13
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    It will when you invoke the class2::display_the_array method.
    OK, I just started reading up on that. I still don't have a full understanding of it, but I'll look into it more.

    Fou-Lu thanks for you help again. You rock.

  • #14
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by gilgimech View Post
    OK, I just started reading up on that. I still don't have a full understanding of it, but I'll look into it more.
    Disregard that. Smoking too much crack. I get it.
    Last edited by gilgimech; 11-23-2011 at 06:57 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •