accessing objects

Permalink
Sorry for the trivial question (to you) but..

My understanding is that objects are instances of a class, which has both methods (functions) and properties (vars and arrays) of various types. Classes can have their methods called and variables can be instantiated with a Class. Classes can be extended but not objects as objects are merely an instance of the class. Child objects can overwrite their parent's property values within their instance. The benefit of passing a variable as an object is so that it's methods go with it and enable their use - instead of writing another sub-routine. (Hopefully, this is correct.) Questions:

1. Accessing values when inside of an object seems difficult to me. Is it only done through an ArrayAccess function, which is essentially the (partial) purpose of the object's methods?

2. When you ask (is_array) are you essentially asking whether it's a multi-dimensional array for the purpose of access it's values?

3. In a world of associative arrays, it would seem the only way to access objects effectively are through methods. I assume that's why C5 makes such an effort with naming conventions. OR, am I missing something obvious...?

Thanks, and I really do appreciate this forum and the people in it - I've read plenty here and learned a lot.

Rick

Ricalsin
 
jordanlev replied on at Permalink Reply
jordanlev
No need to apologize for asking questions :)

It's hard to understand object-oriented programming until you've built larger projects without it and experienced the problems that it solves. The problem is mostly about keeping large codebases organized (as opposed to increasing performance or allowing you to do things that you otherwise couldn't do).

1) I'm not sure what an ArrayAccess function is. You usually access values inside a function like this:
$myobject->myvalue;
or like this:
$myobject->myFunction()

BUT whoever wrote the class can put the word "private" before the variable or the function and that means you CANNOT access that variable or function from outside the object.

If you are inside the object, though, you can always access the object's values and functions (even if they're private) like this:
$this->myvalue;
$this->myFunction();

2) is_array() has nothing to do with classes and objects. It is just a php function that tells you whether or not a variable is an array (as opposed to as string or a number or an object). This seems kind of useless, unless you're writing a function and you want people to be able to pass one value or many values (for example, if you wrote a function that outputs a string to a log file, you could write it to take just one string, or an array of strings -- inside the function you need to check if you were passed the single string or the array of strings so you know how many you need to deal with).

3) Objects and arrays don't really have anything to do with each other (I mean, they're both ways to store variables, but one doesn't interfere with the other). What do you mean exactly by "accessing objects through methods"? If there's a variable in an object that's an array, you can access it like any other variable:
$myobject->myArray;

But you could also have a function return an array:
$myobject->functionThatReturnsArray();

It just depends on how the class was written.

Hope that helps!

-Jordan
Ricalsin replied on at Permalink Reply
Ricalsin
Jordan, this helps. I'm trying to move from book knowledge to applicable knowledge. I've tried what you said and I'm getting the responses you state. Nice.

Because C5 APIs are not documented thoroughly (I've read the threads, I understand the reasons why) I've been trying to ascertain information in this way:

1. I write a foreach loop around get_class_methods($myObject) for each object I am interacting with.

2. Then I write a foreach loop around get_object_vars($myObject) to see the object properties.

This helps me to know what I'm dealing with. Is there a better way to discover this information? I'm wondering if something like Firefox's Firebug could give this information more readily.

I tried installing the zend studio (IDE) with no success and then subsequently read that most good programmers typically choose light weight tools, like Vim (which I can't figure out). I use EditPlus3 and Firefox FTP. Can you advise on my approach and tools? I'm up against the reality of actually "doing" something - but knowing I'm in the ballpark will help.

Thank you for sharing your knowledge and time!

Rick
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Hi Rick,
A more thorough API documentation would be really useful -- I'm sure it will happen eventually, but in the meantime the best way to learn these things is to have a context for WHY they exist. Simply knowing what all of the methods and variables of a class are doesn't always help much, because it doesn't tell you WHY you'd want to use them.

The only way to truly understand what they're all for and why you'd need them is to try to build something that uses those API's. So... is there a certain kind of block you want to create? Or do you have an idea for some kind of page on your website that is more interactive than just displaying content (like a complicated form)?
I don't think it's possible to understand a complete system that is as large and complicated as Concrete5 just by reading the code -- you need to experience the code by working with it.

As for your specific questions -- Firebug is only for debugging Javascript, not PHP. It is very useful, but not for the purpose you're talking about here.

I agree that a lightweight text editor is better than a big IDE. IDE's make a lot more sense for compiled languages like .Net and Java, but are not as necessary (and don't work as well) for PHP.
On the Mac I use TextMate, and on Windows I use KomodoEdit.

So... if you share an idea for something you want to actually build, I'd be happy to give you some pointers on how to go about it.

Good luck!

-Jordan
Ricalsin replied on at Permalink Reply
Ricalsin
Thank you so very, very much. I have a plan, I've read a lot of C5 and have a pretty good idea of what to do. I'm getting my hands dirty now but GREATLY appreciate the feeling that I can get more sophisticated help when that time comes. For now, I'll grind away and get my own "hands-on" education.

Thank you very much, Jordan!

Rick
Ricalsin replied on at Permalink Reply
Ricalsin
Got killed a bit by the OOP structure, but I think I'm getting it. Traditionally, I'd pull any variable out of an array and use it as needed. When I print_r something and see my value (nested in an object) I would then try to get it per my usual (old) way. In the effort to get it, I would use some php function to pull it apart (casting an object into an array) which only furthered my aggravation as the value would then become useless outside of it's class. The OOP architecture is new to me - and frustrating.

My belief is that the OOP structure is what facilitates the entire block concept to work - benefiting from not only reusing your own code, but installing other people's work as well. I guess it also has more security too, as pulling things apart in a malicious way is more difficult(?).

My hands are dirty now, and I'm sweating...(in a programming kind-a-way).

Rick