Passing class to Function

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
KenHorse
New php-forum User
New php-forum User
Posts: 23
Joined: Sat Dec 26, 2015 12:18 pm

Mon Aug 24, 2020 2:41 pm

I'm using php_serial.class.php, called once

Code: Select all

require_once("php_serial.class.php");
and call it $serial:

Code: Select all

$serial = new phpSerial;
in a global.php file that sets up the serial com parameters.

In another file, I include global.php that contains my other code to send and read from the class

In a function, I pass the class:

Code: Select all

function checkInput($serial){
And one in the Function:

Code: Select all

$read = $serial->readPort();
This throws an error: "PHP Fatal error: Uncaught Error: Call to a member function readPort() on string"

If I do the $read outside of the Function, it doesn't throw the error so I assume I'm doing something wrong passing the class to the function?
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 398
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Tue Aug 25, 2020 2:53 am

See how $read is made up by doing

Code: Select all

 echo "<pre>" . print_r($read, 1) . "</pre>; 
My guess $reading isn't a variable though the error message is basically saying that. .
Life is a fig newton of your imagination! https://www.miniaturephotographer.com
Post Reply