Differences between php v7 and v8

Ive tried this correctly (I think?). This is my new code:

// Connect to database
function ew_Connect() {
	$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

$obj = new &ew_Connect();
echo '<pre>';
print_r($obj);
die;

Im getting
PHP Parse error: syntax error, unexpected token “&” in …phpfn6.php on line 1437

Line 1437 is

$obj = new &ew_Connect();

Try either prefixing the function name with & or remove the & from the following:

’ $obj = new ew_Connect();’

The & returns the reference whereas the function returns the object result.

It is seldom the & is required.

Object in PHP are always returned by reference. The & doesn’t add anything here.

Hmm, if my code is:

function ew_Connect() {
	$object = new PDO("mysql:host=###;port=3306;dbname=###", EW_CONN_USER, EW_CONN_PASS);
	$object->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	return $object;
}


$obj = ew_Connect();
echo '<pre>';
print_r($obj);
die;

I get:
PDO Object
(
)

If I add ‘new’ to the $obj = ew_Connect(); line:

$obj = new ew_Connect();

I get the error: PHP Fatal error: Uncaught Error: Class “ew_Connect” not found

That’s because you’re treating functions as objects which they are not. Functions are just that, functions. They could return objects, but they are not objects themselves. Objects are classes that can have functions which we call methods in the OOP world.

To solve your error, it states it in the error itself. Sort of the reason why I love PHP’s error reporting because it isn’t ambiguous and vague like other languages.

This means just that. There is a class called “ew_connect” that you’re trying to instantiate which PHP cannot find. I’ll give you a hint, it’s what I said in my 1st paragraph.

Ah ok, Ive got that array echoing out now. Its returning (Ive hashed sensitive details):

mysqlt_driver_ADOConnection Object
(
[autoCommit] => 1
[transOff] => 0
[transCnt] => 0
[transaction_status] => 1
[nameQuote] => `
[sysDate] => CURDATE()
[sysTimeStamp] => NOW()
[isoDates] => 1
[connectionId] => mysqli Object
(
[affected_rows] => -1
[client_info] => mysqlnd 8.0.13
[client_version] => 80013
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[error_list] => Array
(
)

        [field_count] => 0
        [host_info] => 217.199.187.58 via TCP/IP
        [info] => 
        [insert_id] => 0
        [server_info] => 5.5.5-10.1.48-MariaDB
        [server_version] => 100148
        [sqlstate] => 00000
        [protocol_version] => 10
        [thread_id] => 462848146
        [warning_count] => 0
    )

[record_set] => 
[database] => ##########
[dbtype] => 
[dataProvider] => 
[host] => ###########
[open] => 
[password] => #########
[username] => ########
[persistent] => 
[debug] => 
[debug_console] => 
[debug_echo] => 1
[debug_output] => 
[forcenewconnection] => 
[createdatabase] => 
[last_module_name] => 
[socket] => 
[port] => 3306
[clientFlags] => 0
[sql] => 
[raiseErrorFn] => 
[query_count] => 0
[query_time_total] => 0
[query_list] => Array
    (
    )

[query_list_time] => Array
    (
    )

[query_list_errors] => Array
    (
    )

[_logsql] => 

)

So that looks to be connecting to the db ok?

Correct, if there is no error here, then you’re ok.

Hi guys. Just reigniting this post.

Im pretty much there with upgrading to php8. However theres one directory that I simply cant get working in php8 and needs to stay as php7.

The site is on Plesk. Despite uploading an htaccess file to the relevant folder telling it to behave as php7, phpinfo() still shows it as running as php8. I cant seem to force that 1 directory to run as php7.

How can this be done please?
Thanks

That’s more of a server config issue rather than PHP language - you might be better off starting a new topic in that section. I’d normally move the thread, but the rest of it is more PHP-centric.

Or you could go into a bit more detail about what is stopping you from converting this last part to PHP8 and see who can help with it.

1 Like

Thanks @droopsnoot . I will post in the relevant forum.

A post was split to a new topic: Problems changing from PHP7 to PHP8

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.