Skip to content

A single DB connection instead of multiple connections #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions sites/intro-to-php/creating_a_data_class.step
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ steps do
end

step do
message "Now create a connect function:"
message "Now create a constructor that will assign a PDO instance to a class variable:"

source_code :php, <<-PHP
<?php
class TopicData {
protected $connection;

public function connect()
public function __construct($pdo)
{
$this->connection = new PDO("mysql:host=localhost;dbname=suggestotron", "root", "root");
$this->connection = $pdo;
}
}
?>
Expand All @@ -43,11 +43,11 @@ steps do
<?php
class TopicData {

protected $connection = null;
protected $connection;

public function connect()
public function __construct($pdo)
{
$this->connection = new PDO("mysql:host=localhost;dbname=suggestotron", "root", null);
$this->connection = $pdo;
}

public function getAllTopics()
Expand All @@ -70,10 +70,9 @@ steps do
source_code :php, <<-PHP
<?php
require 'TopicData.php';
$pdo = new PDO("mysql:host=localhost;dbname=suggestotron", "root", "root");

$data = new TopicData();
$data->connect();

$data = new TopicData($pdo);
$topics = $data->getAllTopics();
PHP

Expand Down