eldan88 Posted June 11, 2013 Share Posted June 11, 2013 Hey, I am little bit new to OOP, and I have some difficulties assigning a string to a property through a method, and then calling the property through the method. Below is an example of what I am trying to accomplish, and when I do call the method nothing echo's out. class test1 { public $last_name; public function name() { $this->$last_name="scott"; return $this->last_name; } } $name = new test1(); echo $name->name(); Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/ Share on other sites More sharing options...
Zane Posted June 11, 2013 Share Posted June 11, 2013 (edited) What version of PHP are you using? I don't see why the above wouldn't echo anything, other than version issues Edited June 11, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435269 Share on other sites More sharing options...
eldan88 Posted June 11, 2013 Author Share Posted June 11, 2013 What version of PHP are you using? I don't see why the above wouldn't echo anything, other than version issues Version 5.3.1. Its giving me a Fatal error: Cannot access empty property message. Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435272 Share on other sites More sharing options...
Zane Posted June 11, 2013 Share Posted June 11, 2013 Cannot access empty property message. According to the code you provided, that error is completely true. You did not provide the code that made the error, that is key to getting the best answer. Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435275 Share on other sites More sharing options...
Strider64 Posted June 11, 2013 Share Posted June 11, 2013 (edited) <?php // Using traditional setters and getters: class User { private $username = NULL; public function getUsername() { return $this->username; } public function setUsername($username) { $this->username = $username; } } $data = new User(); $data->setUsername('Justin Verlander'); echo $data->getUsername(); This uses traditional setters and getters method, but what really neat is that you don't need to use them when pulling from mysql , for you can do something like this: // Check against the database: $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username'; $stmt = $pdo->prepare($query); $stmt->execute(array(':username' => $_POST['username'])); $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $stored_user_data = $stmt->fetch(); You just have to make sure that the names in the database corresponds to what in the class: For example public username=Null; must have a matching table column username. I also try to keep name my classes that corresponds in what I doing. Edited June 11, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435276 Share on other sites More sharing options...
Solution kicken Posted June 11, 2013 Solution Share Posted June 11, 2013 $this->$last_name="scott"; That should just be $this->last_name="scott"; You don't use the $ before property names. Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435277 Share on other sites More sharing options...
eldan88 Posted June 12, 2013 Author Share Posted June 12, 2013 <?php // Using traditional setters and getters: class User { private $username = NULL; public function getUsername() { return $this->username; } public function setUsername($username) { $this->username = $username; } } $data = new User(); $data->setUsername('Justin Verlander'); echo $data->getUsername(); This uses traditional setters and getters method, but what really neat is that you don't need to use them when pulling from mysql , for you can do something like this: // Check against the database: $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username'; $stmt = $pdo->prepare($query); $stmt->execute(array(':username' => $_POST['username'])); $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $stored_user_data = $stmt->fetch(); You just have to make sure that the names in the database corresponds to what in the class: For example public username=Null; must have a matching table column username. I also try to keep name my classes that corresponds in what I doing. Thanks a lot. I read through it and it makes a lot of sense. =) Quote Link to comment https://forums.phpfreaks.com/topic/279023-need-help-assigning-a-value-to-a-property/#findComment-1435475 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.