Search the Community
Showing results for tags 'static-properties'.
-
Good morning Freaks, I hope you're all having a productive week. I've a question about an issue I'm having accessing static properties. I've been doing some reading on them but haven't yet deployed one successfully. I have a bunch of classes and I want to make the database query tables all static so they can be used cross-class, so to speak. Here's a sample attempt -> class Post { private $conn; private $user_obj; public static $table = "posts"; public function __construct($conn, $username) { $this->conn = $conn; $this->user_obj = new User($conn, $username); // $this->table = "posts"; } and accessing it here -> public function getBreakingNews() { $query = mysqli_query($this->conn, "SELECT * FROM self::$table WHERE type='breaking' ORDER BY RAND()"); This gets me an error that $table doesn't exist as does using Post::$table. When I assign $this->table = 'posts' (the commented out line in __construct) and access it by $this->table all works great. What am I doing wrong that it won't find that public static property? In the manual there's this example, which I feel like I've stayed true to -> class Foo { public static $my_static = 'foo'; public function staticValue() { return self::$my_static; } } ..... print Foo::$my_static . "\n"; The only difference I see is that they're printing the value and I'm trying to use it in a database query. Can anyone guide me through this mistake I'm apparently making and can't see? I've tried both self::$table and Post::$table, self::table and Post::table, every permutation I can think of but still the variable is saying it's unassigned. Share your knowledge please
- 6 replies
-
- oop
- static-properties
-
(and 1 more)
Tagged with: