Reading a public array from a php page

I am quietly going bonkers. I can use methods and classes fine but attributes I am going lala!

<?php require_once ($_SERVER['DOCUMENT_ROOT'] ."/beta02/inc-head.php"); 
//set up functions and pdo

class Schedule{

public $_day= 'Sending day(s) not set';  
protected $_dayId= 'Sending time not set';         
protected $_transmissionId= 'Sending time not set';       
public $transmissionIdArray = array()  ;       


public function retrieveTransmission($person){  
$this->person = $person;
        try
    {   
        global $pdo;
        $sql = 
        "SELECT id FROM transmission 
        WHERE person_id = :person;";
        $statement = $pdo->prepare($sql);
        $statement->bindParam(':person',$person, PDO::PARAM_INT);
        $statement->execute();
        $result=$statement ->fetchAll(PDO::FETCH_BOTH);
        foreach ($result as $value)
        {
            array_push($this->transmissionIdArray,$value['id']);
            //echo $value["id"];
        }
        $out=$this->transmissionIdArray;
        return($out);
        }
        catch (PDOException $e)
        {
        $output = 'Error getting messages ready to display #6734: ' . $e->getMessage();
        include $_SERVER['DOCUMENT_ROOT'] ."/vvvxxx/includes/output.html.php";
        exit();
        } 

debug ($this->transmissionIdArray,"trans array");

    }
}

This works fine … up to a point!

$myClass= new Schedule;
$xxxx = $myClass->retrieveTransmission (2);
echo implode ($xxxx);

Works fine. But how do I access public scalars like $day or, more importantly arrays. This is just during the building and testing phase. Once working vaguely OK they will go back to being protected.

I tried:

$xxxx=$myClass->$transmissionIdArray;
echo implode ($xxxx);

I tried (optimistically):

echo implode ($transmissionIdArray);
and 
$xxxx=$myClass->$day...

But could get nothing to work. So newb question: How do I simply grab public values (esp arrays) from an instance of a class?

I know this must be in Class class 101 but I am old and forgetful and have wasted about an hour looking for the answer (might have something to do with four hours sleep).

TIA for any help.

PS I would never DARE to post something like this on Stack Exchange

PPS Is there any way to make the posting text area bigger. Really horrible to try and post while looking through a tiny letterbox!

Did you put this code after creating an instance of the class (new) and after calling the method? From the code you posted the attribute will be an empty array until you call the method.

Yes to instance and methods work fine It is just accessing the variables I am stumbling with.

This is the point of the exercise. I am trying to read the PUBLIC attributes (variables). I thought that was the point of public variables that you could just call them anywhere once you have instantiated a clas … (This is just for debugging.).

Thanks for coming back…

Unless you define the variable as static there will be one instance per object. You will need to instatiate one or more objects from the class before being able to reference their properties.

The OP did, instantiate an object.

They are just trying to access the property of said object.

Instead of $xxxx=$myClass->$day, you’re trying to reference $xxxx=$myClass->_day (incidentally, this is not a normal naming convention - underscores are normally reserved for Protected/Private properties.)

$myClass->$day is trying to read the property of $myClass that is named whatever is stored in your $day variable in local scope.
$myClass->day is trying to read the property of $myClass that is named day.

1 Like

@StarLion that was the problem. Have moved on (never did solve the prob) (5 mins later tried again and YAYYYYY it worked!).

The _var rather than var was because it was a protected variable that I was temporarily making public!

@felgall I am just trying to discover what the heck is going on inside a funky Class method :smile:

Thanks all for your input guys. Genius!