fredyap1234 Posted April 8, 2010 Share Posted April 8, 2010 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 Quote Link to comment Share on other sites More sharing options...
trq Posted April 8, 2010 Share Posted April 8, 2010 Mind posting some relevant code? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted April 8, 2010 Share Posted April 8, 2010 Is there a return statement in the showname() function? Or are you just issuing an echo statement in showname()? Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted April 8, 2010 Share Posted April 8, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.