Page 1 of 1
Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 8:53 pm
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.
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 8:59 pm
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:
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];
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:13 pm
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'];
?>
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:18 pm
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
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:20 pm
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!
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:21 pm
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/
Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:34 pm
by JAB Creations
Downloaded, tried it, broke XHTML thus it obviously doesn't use
htmlspecialchars.
So now I'm debugging a debugger.

Re: Access key/value from MySQL array inside PHP class?
Posted: Sat Feb 14, 2009 10:51 pm
by JAB Creations
That site has a form but I'm not sure it's a contact form, cheers for unusable minimalism!
Any way what the heck is this...
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.
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.
Looking at the debugger info yeah, that's pretty cool.

Re: Access key/value from MySQL array inside PHP class?
Posted: Sun Feb 15, 2009 12:41 am
by josh
Also check out firePHP + firebug ( Zend has firebug integration with Zend_Log )