Access key/value from MySQL array inside PHP class?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Access key/value from MySQL array inside PHP class?

Post by JAB Creations »

I know how to access a key and value from a regular array...

Code: Select all

<?php
echo $my_array[0];
echo $my_array['my_value'];
?>
However I'm not sure how to access the same information from an array when it's stored in a class variable. Here are some of my attempts...

Code: Select all

<?php
echo $class_var['example'];
echo $class_var[0];
echo $class_var->get('example')['example'];
echo $class_var->get('example')'example];
echo $class_var->get('example')[0];
echo $class_var->get('example'['example']);
echo $class_var->get('example'[example]);
echo $class_var->get('example'[0]);
?>
I'm not really sure what else to try. I'm also not entirely sure how public or private commands work in relation or if they are not related to this plus I get nothing but education debate pages when I do a search on that bit any way.

Thoughts please?

PS - I have not coded for an entire week straight so I am not going directly back to messing around with Zend. I will be doing so after I have had my fill of progress so this week after the weekend.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Access key/value from MySQL array inside PHP class?

Post by Eran »

You are trying to use the array operator on a function, which is not allowed in PHP. If you were really using it on an array member it would have worked:

Code: Select all

$object -> example[0];
If you want to use it with a function, pass the key to the function or use a temporary variable:

Code: Select all

class Example {
    public $data = array(0,1);
    public function get($key = null) {
        if(is_int($key)) {
            return $this -> data[$key];
        } else {
            return $this -> data;
        }
    }
}
 
$example = new Example();
$example -> data[0];
$example -> get(0);
$array = $example -> get();
$array[0];
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Access key/value from MySQL array inside PHP class?

Post by JAB Creations »

Thanks pytrin! I tried something new today with classes that I should have done a long time ago...good examples are how I learn after all!

So I tried to simply echo the class which didn't work. I then tried to echo class_name->class_variable which worked! I always use print_r however I only tried directly applying it to a class (without a class variable) for the first time tonight oddly enough.

The issue that prevented me from doing this the normal way was when I used to talk with another PHP programmer he gave my the following code...

Code: Select all

<?php
class example_class
{
 public function set($name,$value) {$this->$name = $value;}
 public function get($name){return $this->$name;}
}
?>
I was used to using the functions. It's stuff like this which is why I want to avoid frameworks (learning a framework and then thinking that is the way to code end of story) but that's off-topic here.

Any way so the following works...

Code: Select all

<?php
echo $class_example->class_variable['array_item'];
?>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Access key/value from MySQL array inside PHP class?

Post by Eran »

I always use print_r
Switch to var_dump. More information, and works on objects as well. You can wrap it with <pre> tags for better formatting
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Access key/value from MySQL array inside PHP class?

Post by JAB Creations »

I also have started using var_dump though I wouldn't say as often as print_r. I've also started using the pre element to format things especially longer strings that get blocked because they don't wrap. Thanks again!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Access key/value from MySQL array inside PHP class?

Post by josh »

You shouldn't access members publicly like that, learning to do OOP is about behavior & data, not just data like arrays. Let's say you wanted to run some behavior every time the property was accessed, for example. Keeping things encapsulated behind getters and setters is good practice

http://dbug.ospinto.com/
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Access key/value from MySQL array inside PHP class?

Post by JAB Creations »

Downloaded, tried it, broke XHTML thus it obviously doesn't use htmlspecialchars.

So now I'm debugging a debugger. :lol:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Access key/value from MySQL array inside PHP class?

Post by JAB Creations »

That site has a form but I'm not sure it's a contact form, cheers for unusable minimalism! :roll:

Any way what the heck is this... 8O
language="JavaScript"
I replaced that with type attribute obviously and then added //<![CDATA[ and //]]> respectively. Also there were some missing quotes (what is cellpadding and cellspacing?) Yes...that's a rhetorical question. :P

Any way now that I debugged the debugger though the contact form looks like it may be a "tell a friend" form with a header "spread the word" so I simply did a whois and emailed the author directly with the fixes I implemented and a nice test case with code. It's useful to label forms accordingly otherwise people won't want to use them. :wink:

Looking at the debugger info yeah, that's pretty cool. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Access key/value from MySQL array inside PHP class?

Post by josh »

Also check out firePHP + firebug ( Zend has firebug integration with Zend_Log )
Post Reply