Asked 7 years ago
23 May 2017
Views 1081
jessica

jessica posted

how to acess the property of object in PHP ?

how to acess the property of object in PHP ?



class Customers_List  {

	/** Class constructor */
	
	public $tablename='';
 
	public function __construct($tablename='') {
	
		$this->tablename=$tablename;
        }

       
	public static function get_customers( $per_page = 5, $page_number = 1 ) {

		  $sql = "SELECT * FROM {$wpdb->prefix}".$this->tablename;
       }
}



class for customers , its accessing the $tablename property in another function of the class .

so its give me error like this



Fatal error: Using $this when not in object context in
steave

steave
answered Apr 24 '23 00:00

In PHP, you can access the properties of an object using the arrow (->) operator. Here's a brief example:

1.Define a class with some properties:


class Person {
  public $name;
  public $age;

  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}

2.Create a new instance of the class and assign it to a variable:


$person = new Person("John", 30);

3.Access the properties of the object using the arrow operator:


echo $person->name; // Outputs "John"
echo $person->age; // Outputs 30

In this example, we define a Person class with two properties ( name and age ). We then create a new instance of the Person class and assign it to the $person variable. To access the properties of the $person object, we use the arrow operator (->) followed by the property name (e.g., $person->name). This allows us to read or modify the values of the object's properties.

It's worth noting that the arrow operator can also be used to call methods on an object, in addition to accessing its properties.
Post Answer