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)
} 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
public and private only work inside of a class, are those class methods or just bare functions?
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; } } }
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; } } }
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:
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
$stmt = $this->conn->query($sql); $data = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $data[] = $row; } return $data; } } This is a class ProductGateway
so the first error went away then?
when you removed
private
from the method signature?Yes than I got this Call to a member function getAll() on null
Yes
I removed
right
well, you're not setting the gateway property, you're just passing it to the constructor
try this
Maybe I can send you folder with all code if it's posible
Okay I'm gona try
All good
Thanks man alote
š
no worries, glad to help š
I forgot to include that
actually, I've been looking into this a little more, and your original syntax should work in PHP8. What version are you using? @kekadirektor
Older versions
That's why I think
I need to install new one
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
Okay, I'm gona install. Thank you again