Jump to content

syntax error unexpected 'public' (t_public)


LeonLatex

Recommended Posts

I can't find the error. In VSC the error is only on only line 17. In DW, which the image is from, the error is on multiple lines (as you can see on the image.
Can someone please help me and tell me where/what the error is?

I have checked the PHP reference manual + websites. They all tell me teres nothing wrong.

 

2023-04-20.thumb.png.3b8c9b020c0485e03b001a15df8b7e01.png

Link to comment
Share on other sites

3 hours ago, LeonLatex said:

I have checked the PHP reference manual + websites. They all tell me teres nothing wrong.

You can't always just blindly trust errors in editors.  The editor might not know about newer syntax structures, or might be linting against a specific version of PHP.  The constructor promotion feature you are using is new to PHP 8, so if your editor is linting against PHP 7 it'll correctly see that as an error.

Do you get an error when actually trying to run the code?  If so, what is that error?  Are you using PHP 8 or PHP 7 to run the code?

Link to comment
Share on other sites

43 minutes ago, cyberRobot said:

If you hover over the red line numbers in Dreamweaver, it will likely say something like "syntax error, unexpected private (T_PRIVATE)". Basically, it doesn't like "private" before each of the arguments for the class method.

Do you mean DW doesn't like it? So there is nothing wrong? The script doesn't run in the browser when I run this in the top:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

13 minutes ago, kicken said:

You can't always just blindly trust errors in editors.  The editor might not know about newer syntax structures, or might be linting against a specific version of PHP.  The constructor promotion feature you are using is new to PHP 8, so if your editor is linting against PHP 7 it'll correctly see that as an error.

Do you get an error when actually trying to run the code?  If so, what is that error?  Are you using PHP 8 or PHP 7 to run the code?

PHP 7.2 is running on the server. I can upgrade to 8.1, but then i will get a lot of problems with other scripts and sites.

As I told cyberRobot, the script won't run in the browser. I try with this one too:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

Edited by LeonLatex
Link to comment
Share on other sites

Add this to the top of your script:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "In my script at line: ".__LINE__."<br>";

Let's see if you get that far at least.  If not then you are not showing us enough of your code.  PLEASE do not post an 'image' or your code - only real code that we can easily read and copy to our own editors to work with if necessary.

Link to comment
Share on other sites

1 hour ago, LeonLatex said:

PHP 7.2 is running on the server

 

1 hour ago, kicken said:

The constructor promotion feature you are using is new to PHP 8

You can't use a PHP 8 feature while running PHP 7.2.  Either adjust your code to be compatible with PHP 7.2, or upgrade your server to PHP 8.

Upgrading would be better, since support for PHP 7.2 ran out 2 years ago.

 

Edited by kicken
Link to comment
Share on other sites

53 minutes ago, kicken said:

 

You can't use a PHP 8 feature while running PHP 7.2.  Either adjust your code to be compatible with PHP 7.2, or upgrade your server to PHP 8.

Upgrading would be better, since support for PHP 7.2 ran out 2 years ago.

 


I tried to upgrade. It didn't work. I try to write the script again.

Link to comment
Share on other sites

45 minutes ago, ginerjm said:

If you do re-write it and still have errors please post it to here as TEXT and not a picture of text.  That way we can work with it.

 

Ok, here it is. Thanks for the CTRL+Z. I re-wrote the script, but it was more errors than the first one, so I post the old one. The same errors and amount of them.

<?php
class databaseTable {
	public function __construct(private PDO $pdo, private string $table, private string $primaryKey) {
}
public function find($field, $value) {
	$query = 'select * FROM `' . $this->table . '` WHERE `' . $field . '` = :value';
	
	$values = [
		'value' => $value
	];
	
	$stmt = $this->prepare($query);
	$stmt->execute($values);
	
	return $stmt->fetchAll();
}
public function findAll() {
	$stmt = $this->pdo->prepare('SELECT * FROM `' . $this->table . '`');
	$stmt->execute();
	
	return $result->fetchAll();
}
	
public function total() {
	$stmt = $this->pdo->prepare('SELECT COUNT(*) FROM `' . $this->table . '`');
	$stmt->execute();
	$row =  $stmt->fetch();
	return $row[0];
}
	
public function save($record) {
	try {
if (empty($record[$this->primaryKey])) {
			unset($record[$this->primaryKey]);
			}
			$this->insert($record);
			} catch (PDOException $e) {
			$this->update($record);
				}
			}
			private function update($values) {
				$query = ' UPDATE `' . $this->table .'` SET ';
				
				foreach ($values as $key => $value) {
					$query .='`' . $key . '`= :' . $key . ',';
				}
				
				$query = rtrim($query, ',');
				
				$query .= ' WHERE `' . $this->primaryKey . '` = :primaryKey';
				
				//Set the primary key variable
				$values['primaryKey'] = $values['id'];
				
				$values = $this->processDates($values);
				
				$stmt = $his->pdo->prepare($query);
				$stmt->execute($values);
			}
												
			private function insert($values) {
				$query = 'INSERT INTO `' . $this->table . '`(';
			}
												
			foreach ($values as $key => $value) {
				$query .= '`' . $key . '`,';
			}
			
			$query = rtrim($query, ',');

			$query .= ') values (';
			foreach ($values as $key => $value) {
				$query .= ':' . $key . ',';
			}
			
			$query = rtrim($query, ',');
												
			$query .= ')';
												
			$values = $this->processDates($values);
			
			$stmt = $this->pdo->prepare($query);
			$stmt->execute($values);
			}
			
			public function delete($field, $value) {
				$values = [':value' => $value];
				
				$stmt = $this->pdo->prepare('DELETE FROM`' . $this->table . '` WHERE `' $field . '` = :value');
				
				$stmt->execute($values);
			}
			function processDates($values) {
				foreach ($values as $key => $value) {
					if ($value instanceof DateTime) {
						$values[$key] = $value->format('Y-m-d');
				}
			}
				
			return $values;
			}
		}

 

Link to comment
Share on other sites

You have an extra brace in your code near the insert method, part that was not visible in your original image.  Your crazy indentation is not helping to spot such errors, you need to work on formatting better.  If your editor is causing the crazy formatting, that's usually a good sign your braces or quotes don't match up properly.  There are some other small errors, maybe once you fix the brace issue they will be easier for you to spot.

Again, you need to stop using that PHP 8 only feature as well if you're not going to upgrade.

 

Edited by kicken
Link to comment
Share on other sites

30 minutes ago, kicken said:

You have an extra brace in your code near the insert method, part that was not visible in your original image.  Your crazy indentation is not helping to spot such errors, you need to work on formatting better.  If your editor is causing the crazy formatting, that's usually a good sign your braces or quotes don't match up properly.  There are some other small errors, maybe once you fix the brace issue they will be easier for you to spot.

Again, you need to stop using that PHP 8 only feature as well if you're not going to upgrade.

 

Thanks. Hope this will bring me on the right path.

Link to comment
Share on other sites

FYI - Looking into the DW issue a bit more, I guess only PHP 5.6 and PHP 7.1 are supported for error detection. More information, including how to switch which version is used, can be found here:
https://helpx.adobe.com/dreamweaver/using/setting-coding-environment.html#SupportforPHP56and71versions

The default appears to be PHP 5.6. At least that's what mine was set to...and I installed DW within the last month or so. I switched to VS Code several years ago and apparently never noticed how outdated DW is.

Link to comment
Share on other sites

I remember when I was in college the classes I took required Dreamweaver, but all the instructors would say "Even thought the syllabus and I'm required to show how to use Dreamweaver when you get into  the real world ditch Dreamweaver and use a real IDE". That was many moons ago and even back then all the students groaning having to pay for Dreamweaver.  😂  Back then it wasn't subscription based, but we at least received a student discount. 

Link to comment
Share on other sites

I primarily used Dreamweaver for hand-coding purposes. I never really used the WYSIWYG side because of the experience I had with Microsoft FrontPage. So much wasteful code in a time when file size of the HTML files being downloaded was more important. I think I only started using Dreamweaver because it was bundled with other Macromedia software I needed for class projects. Plus, it was much better than coding everything in Notepad...and whatever I used later on that performed some basic color coding.

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.