eldan88 Posted March 28, 2014 Share Posted March 28, 2014 Hey Guys. I have created a method in my checkout_model_class.php, which generates table rows every time I instantiate a new object. In the second the parameters I am passing an input field field. On that input field I am trying to access a property from another class so that I can enter in the value attribute. When I try to access that property I get the following error "Fatal error: Using $this when not in object context in etc...." I'm thinking the reason for that is because I am trying to access a property in another class. Should I access these properties statically? Below is my code. Any help would be really appreciated. form.php (this is the page where I am calling my $_POST properties from my Users class and where I am instantiating my table rows from my checkout_model_class.php) $users = new Users(); // This Users class is in the users.php page if(isset($_POST['submit'])) { $users->post_values(); // The post values method does assigns all the post values to the properties of the user class. //validation code goes here } <form action="#" method="POST"> <table> <?php $table = new create_checkout_table(); echo $table->table_rows_and_columns("Name:","<input type=\"text\" name=\"name\" value=\"{$this->name}\">"); // the table_rows_and_columns is located in the checkout_model_class.php </table> </form> users class <?php require_once("../include/functions.php"); class Users { public $user_id = ""; public $name = ""; public $phone = ""; public $address = ""; public $suite = ""; public $cross_streets = ""; public $email = ""; public $credit_card_number = ""; public $exp_month = ""; public $exp_year = ""; public function __construct() { // condition will load the registered users values into the input fields of the //this will be checked automatically when the page loads // The getUsers functions retrieves the registered users information from the database so that it can populate the values inside the text field... this is not fully implemented.. yet if (user_logged_in()) { $this->user_id = $_SESSION['logged']['id']; $this->name = getUsers($user_id, "full_name"); $this->phone = getUsers($user_id, "phone"); $this->address = getUsers($user_id, "address"); $this->suite = getUsers($user_id, "suite_app"); $this->cross_streets = getUsers($user_id, "cross_streets"); $this->email = getUsers($user_id, "email"); $this->credit_card_number = getUsers($user_id, "hashed_cardnumber"); $this->exp_month = getUsers($user_id, "exp_month"); $this->exp_year = getUsers($user_id, "exp_year"); }// end of function __construct() } // end of if (user_logged_in()) //This function is used right after if if(isset($_POST['submit'] and will grab all the post values and assign them to the attributes above so that they can be called later in the code. public function post_values() { $this->name = $_POST['name']; $this->phone = $_POST['phone']; $this->address = $_POST['address']; $this->suite = $_POST['suite']; $this->cross_streets = $_POST['cross_streets']; $this->email = $_POST['email']; $this->credit_card = $_POST['credit_card_number']; $this->exp_month = $_POST['card_expiry_month']; $this->exp_year = $_POST['card_expiry_year']; }//public function post_values() }// end of users class checkout_model_class.php(This is where I instantiate the table rows) class create_checkout_table { //@data_name just a regular string that displays text that is correlated to the text input field //@data - This is where all the input text fields gets passed in as an argument. public function table_rows_and_columns($data_name="",$data="") { $row = "<tr><td>{$data_name} </td> <td>{$data}</td></tr>".PHP_EOL; return $row; }// end of function table_rows_and_columns Link to comment https://forums.phpfreaks.com/topic/287369-need-help-calling-properties-within-another-class/ Share on other sites More sharing options...
maxxd Posted March 28, 2014 Share Posted March 28, 2014 'name' is a Users() property. In order to access it, you'll need to use $users->name instead of $this->name in form.php, just as you do with the $table instance of the create_checkout_table() class. Link to comment https://forums.phpfreaks.com/topic/287369-need-help-calling-properties-within-another-class/#findComment-1474320 Share on other sites More sharing options...
eldan88 Posted March 28, 2014 Author Share Posted March 28, 2014 Hey Maxxd!!!! Thank you very much! I tried that and it worked I didn't realize that before!! Thanks again!! Link to comment https://forums.phpfreaks.com/topic/287369-need-help-calling-properties-within-another-class/#findComment-1474340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.