Running functions from one class in another Ch8

support
book

#1

I'm stuck at ch. 8 of PHP & MySQL: Novice to Ninja.
I'm unable to get the functions from my functions class to run in the controller. The book has you put the code for your joke list (and other pages) into a class so all the pages on the site can be run on a single page. Our tables are set up in another class which is passed the connection to the database.

class JokeController {
    private $authorsTable;
    private $jokesTable;
    

    public function __contruct($jokesTable, $authorsTable){
        $this->jokesTable = $jokesTable;
        $this->authorsTable = $authorsTable;
    }

   public function list() {
   $result = $this->jokesTable->findAll(); //Problem running this function

        $jokes =[];
        foreach ($result as $joke){
            $author = $this->authorsTable->findById($joke['authorid']);

            $jokes[] = [
                'id' => $joke['id'],
                'joketext' => $joke['joketext'],
                'jokedate' => $joke['jokedate'],
                'name' => $author['name'],
                'email' => $author['email']
            ];
        }

        $title = 'Joke List';
        
        $totalJokes = $this->jokesTable->total();
        
        


        return ['template'=>'jokes.html.php',
        'title'=>$title,
        'variables'=> [
            'totalJokes'=>$totalJokes,
            'jokes'=>$jokes
        ]
    ];
    }`

I get a fatal error when this tries to run. From what I could find on stack overflow this is because the connection isn't being correctly used. I'm using PHP 7.1.9


#2

Hi @masnhale, can you post the exact error you are getting?

If the problem is at the line

  $result = $this->jokesTable->findAll()

Check that the $pdo instance is correctly being passed into the DatabaseTable class' constructor.


#3

Hi @TomB Thanks for replying. This is the error I get.

The code I have written is below. From what the book says, I believe I am putting the $pdo in the class correctly

This is my index.php

<?php 
function loadTemplate($templateFileName,$variables=[]){
    extract($variables);

    ob_start();
    include __DIR__ . '/../templates/' . $templateFileName;
    
    return ob_get_clean();
}
try{
    require __DIR__ . '/../includes/connection.php';
    include __DIR__.'/../classes/DatabaseTable.php';
    include __DIR__ .'/../controllers/JokeController.php';

    $jokesTable = new DatabaseTable($pdo,'joke','id');
    $authorsTable = new DatabaseTable($pdo,'author','id');

    $jokeController = new JokeController($jokesTable, $authorsTable);
    
    $action = $_GET['action'] ?? 'home';

    $page = $jokeController->$action();

    $title = $page['title'];

    if(isset($page['variables'])){
        $output = loadTemplate($page['template'], $page['variables']);
    }else {
        $output = loadTemplate($page['template']);
    }
 


 
} catch(PDOException $e) {
    $title = 'An error has occured';
    $output = 'Database error: ' . $e->getmessage() . 'in ' . $e->getFile() . ': ' . $e->getLine();
} 
include __DIR__ . '/../templates/layout.html.php';

?>

This is my connection.php

<?php 
	$pdo = new PDO('mysql:host=localhost;dbname=nov2ninja;charset=utf8','jokeUser','mypassword');
	$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

The jokeController class vars and contructor

class JokeController {
    private $authorsTable;
    private $jokesTable;
    

    public function __contruct($jokesTable, $authorsTable){
        $this->jokesTable = $jokesTable;
        $this->authorsTable = $authorsTable;
    }

And lastly the DatabaseTable.php vars and constructor

<?php 
class DatabaseTable{
public $pdo;
private $table;
private $primaryKey;

public function __construct(PDO $pdo, string $table, string $primaryKey){
    $this->pdo = $pdo;
    $this->table = $table;
    $this->primaryKey = $primaryKey;
}

The functions work fine on their own but don't work at all when run from an external object like jokeController. Thanks for helping me out! Other than this I love the book so far, it's really helpful!


#4

With the use of a few var dumps I learned that the variables in the class were returning null. By setting the variables with:

$JokesController->jokesTable = $jokesTable

it worked... At this point I looked over my constructor and discovered I had a typo on the method '__construct' .

Thanks for your help though!


#5

I'm glad you got it working, it's an easy mistake to make!


#6

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