0

if i do

var_dump( $poll->vote_form() );  

class method returns an array which is what i want

array (size=3)
  'id' => int 41
  'name' => string 'sg' (length=2)
  'options' => 
    array (size=4)
      116 => string 'dsg' (length=3)
      117 => string 'dsg' (length=3)
      118 => string 'dg' (length=2)
      119 => string 'gd' (length=2)

but when i do

echo $poll->vote_form['name'];

i get error

Undefined property: poll::$vote_form

hm what seems to be the problem? this is the method of class poll

public function vote_form()
{
            ...
    $form = array('id' => $poll_id, 'name' => $poll_name, 'options' => $poll_options);
    return $form;
}

2 Answers 2

1

try this

$data=$poll->vote_form();
echo $data['name'];
Sign up to request clarification or add additional context in comments.

4 Comments

works perfect :) although i would like to know why is this needed hm :)
You jsut called the method of the poll calss and stores whatever it returned in a variable. SO now $data is an array and you can access it as a normal array now
Because in your code example you try to access a class property called $vote_form instead of a method vote_form(). With PHP > 5.4 you can also write $poll->vote_form()['name']; but my personal oppinion is that you should work with Marko's solution for compatibility with older PHP versions.
I understand that, but dunno why it doesnt work without added code. anyway, ty :)
1

try this:

echo $poll->vote_form()['name'];

2 Comments

well funny thing, i had that code yesterday and it worked perfectly but on different computer (it could be because php version is higher?). On this computer it throws error - Parse error: syntax error, unexpected '[',....
If this doesn't work then use @Let me see's solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.