Jump to content

Need help calling properties within another class


eldan88
Go to solution Solved by maxxd,

Recommended Posts

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
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.