Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2011, 05:24 PM   PM User | #1
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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?
gilgimech is offline   Reply With Quote
Old 11-07-2011, 05:59 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gilgimech (11-08-2011)
Old 11-07-2011, 10:13 PM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Users who have thanked Spookster for this post:
gilgimech (11-08-2011)
Old 11-08-2011, 10:19 AM   PM User | #4
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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.
gilgimech is offline   Reply With Quote
Old 11-08-2011, 01:25 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gilgimech (11-08-2011)
Old 11-08-2011, 01:47 PM   PM User | #6
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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?
gilgimech is offline   Reply With Quote
Old 11-08-2011, 03:44 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gilgimech (11-08-2011)
Old 11-09-2011, 04:58 AM   PM User | #8
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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.
gilgimech is offline   Reply With Quote
Old 11-23-2011, 05:43 PM   PM User | #9
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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..
gilgimech is offline   Reply With Quote
Old 11-23-2011, 05:48 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gilgimech (11-23-2011)
Old 11-23-2011, 06:04 PM   PM User | #11
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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?
gilgimech is offline   Reply With Quote
Old 11-23-2011, 06:06 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It will when you invoke the class2::display_the_array method.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gilgimech (11-23-2011)
Old 11-23-2011, 06:18 PM   PM User | #13
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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.
gilgimech is offline   Reply With Quote
Old 11-23-2011, 06:30 PM   PM User | #14
gilgimech
New Coder

 
Join Date: Mar 2010
Posts: 58
Thanks: 10
Thanked 3 Times in 3 Posts
gilgimech is an unknown quantity at this point
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..
gilgimech is offline   Reply With Quote
Reply

Bookmarks

Tags
array, classes, oop

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:56 PM.


Advertisement
Log in to turn off these ads.