Jump to content

Assign a variable with accessed methods


fredyap1234

Recommended Posts

Hi guys,

 

I'm a beginning self learner in PHP and I'm looking for a solution to one of my problem. I've got a book from A-press recently for beginner and I'm trying out some of the tutorials in it.

 

For now my php project have a class and there's an object that accesses the class' method.

 

$n->showname();

 

However, I would like to assign this to a variable and perhaps push it into an array. This is how I did it.

 

$name = $n->showname();

 

This is not working of course and you guys are probably laughing at me now.

 

So is there any other solutions for this if I were to insist on placing it into a variable? Is it a good practice? Don't get me wrong guys, I'm not expecting an easy spoon-fed answer. Perhaps a good hint would be more than helpful.

 

Thanks in advance guys.

Fred

Link to comment
Share on other sites

It's better practice to have the function getName() to return private variables, as that name should be. Simply replace your echo with return and then it shall work. Then to  place the name in an array create a variable like $person = array('name' -> $name); then it shall work. Then to add other values to the array $person['age'] = $age etc.

 

On a side note since you are just beginning, you shouldn't head right in to OOP as it can be confusing if you don't know the basics.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.