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.
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!
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.
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).
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.
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.
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.