SYNTAX ERROR PROBLEM INSIDE OOP PHP

My problem is: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) When I do this: public function __construct(private ProductGateway $gateway) {
} and call $getway there: private function processCollectionRequest(string $method): void { switch($method) { case "GET": echo json_encode($this->gateway->getAll()); break; } } I got this error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE)
14 Replies
Jochem
Jochemā€¢12mo ago
public and private only work inside of a class, are those class methods or just bare functions?
Keka Direktor
Keka Direktorā€¢12mo ago
class methods <?php class ProductController {
public function __construct(private ProductGateway $gateway) {
} public function processRequest(string $method, ?string $id): void { if ($id) { $this->processResourceRequest($method, $id); } else { $this->processCollectionRequest($method); } } private function processResourceRequest(string $method, string $id): void {
} private function processCollectionRequest(string $method): void { switch($method) { case "GET": echo json_encode($this->gateway->getAll()); break; } } }
Jochem
Jochemā€¢12mo ago
also, it's been a while, but I'm pretty sure you can't mark function arguments as private so your constructor signature should be:
public function __construct(ProductGateway $gateway)
public function __construct(ProductGateway $gateway)
Keka Direktor
Keka Direktorā€¢12mo ago
Call to a member function getAll() on null I get this error class ProductGateway { private PDO $conn; public function __construct(Database $database) { $this->conn = $database->getConnection(); } public function getAll(): array { $sql = "SELECT * FROM signup";
$stmt = $this->conn->query($sql); $data = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $data[] = $row; } return $data; } } This is a class ProductGateway
Jochem
Jochemā€¢12mo ago
so the first error went away then? when you removed private from the method signature?
Keka Direktor
Keka Direktorā€¢12mo ago
Yes than I got this Call to a member function getAll() on null Yes I removed
Jochem
Jochemā€¢12mo ago
right well, you're not setting the gateway property, you're just passing it to the constructor try this
private ProductGateway $gateway;

public function __construct(ProductGateway $gateway) {
$this->gateway = $gateway;
}
private ProductGateway $gateway;

public function __construct(ProductGateway $gateway) {
$this->gateway = $gateway;
}
Keka Direktor
Keka Direktorā€¢12mo ago
Maybe I can send you folder with all code if it's posible Okay I'm gona try All good Thanks man alote šŸ™‚
Jochem
Jochemā€¢12mo ago
no worries, glad to help šŸ™‚
Keka Direktor
Keka Direktorā€¢12mo ago
I forgot to include that
Jochem
Jochemā€¢12mo ago
actually, I've been looking into this a little more, and your original syntax should work in PHP8. What version are you using? @kekadirektor
Keka Direktor
Keka Direktorā€¢12mo ago
Older versions That's why I think I need to install new one
Jochem
Jochemā€¢12mo ago
you really should, PHP7 is out of support entirely even 8.0 is out of support I think, you should really be using 8.2
Keka Direktor
Keka Direktorā€¢12mo ago
Okay, I'm gona install. Thank you again
Want results from more Discord servers?
Add your server