LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2019, 01:47 PM   #1
gacl
Member
 
Registered: Feb 2004
Posts: 57

Rep: Reputation: 23
Jump Start PHP: Is $i necessary? Is this an array to object?


Hi,

I'm looking at page 73 of Jump Start PHP (http://bedford-computing.co.uk/learn.../09/jsphp1.pdf) and I have a couple of questions:

1. Why is the loop using $i if we're not checking its value?

2. The line next to last... is it converting the array to the class?

Thank you for your help.
 
Old 07-06-2019, 02:54 PM   #2
individual
Member
 
Registered: Jul 2018
Posts: 234

Rep: Reputation: 177Reputation: 177
Code:
<?php
class Posts {
    public $db = '';
    public function __construct() {
        $this->db = new PDO("mysql:host=localhost;dbname=kickstart", "root", "i8vfbxdb");
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->index();
    }

    public function index() {
        $id = 0;
        $posts = array();
        $template = '';
        if (!empty($_GET['id'])) {
            $id = $_GET['id'];
        }
        try {
            if (!empty($id)) {
                $query = $this->db->prepare("SELECT * FROM posts WHERE id = ?");
                $params = array($id);
                $template = 'single-post.php';
            } else {
                $query = $this->db->prepare("SELECT * FROM posts");
                $params = array();
                $template = 'list-posts.php';
            }
            $query->execute($params);
            for ($i = 0; $row = $query->fetch(); $i++) {
                $posts[] = array('id' => $row['id'], 'content' => $row['content']);
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        $query->closeCursor();
        $db = null;
        require_once($template);
    }
}

$posts = new Posts();

?>
1. I don't know why the author chose to use a for-loop like that, since, as you point out, he doesn't use $i. A better way would be a while-loop.
Code:
while ($row = $query->fetch()) {
    ....
}
2. Assuming you mean the line that says $posts = new Posts(), that's creating a new Posts object.

3. A better way of handling the post id would be to assign $_GET['id'] to $id, and then check if it's empty.
Code:
$id = $_GET['id'];
if (!empty($id)) {
    ....
} else {
    ....
}
4. I don't know what the purpose of setting $db to null is. You will only be able to call the index method once, since $db only gets initialized in the constructor.

Last edited by individual; 07-06-2019 at 02:56 PM. Reason: Added the code snippet the OP is talking about.
 
Old 07-06-2019, 03:13 PM   #3
gacl
Member
 
Registered: Feb 2004
Posts: 57

Original Poster
Rep: Reputation: 23
Thank you.

The $posts from the loop has nothing to do with the $posts outside of the class, am I right? In other words, you can set different values, types, etc. for each...? I'm assuming this but I'm not sure...
 
Old 07-06-2019, 03:55 PM   #4
individual
Member
 
Registered: Jul 2018
Posts: 234

Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by gacl View Post
Thank you.

The $posts from the loop has nothing to do with the $posts outside of the class, am I right? In other words, you can set different values, types, etc. for each...? I'm assuming this but I'm not sure...
You're correct that the two variables aren't the same, but I don't remember if php uses global or lexical scoping by default. Put simply, with global scope, variables are accessible throughout the program and can be modified throughout the program. With lexical scope, variables inside a block (class, function, loop, if-statement) are only accessible/modifiable for the lifetime of the block. To avoid confusion it would probably be better to rename the class to Post, and rename the "outside" $posts variable to $post.

Last edited by individual; 07-06-2019 at 03:57 PM. Reason: Clarification
 
Old 09-14-2019, 01:01 PM   #5
gacl
Member
 
Registered: Feb 2004
Posts: 57

Original Poster
Rep: Reputation: 23
So I looked this up and I can confirm that in PHP the scope of a variable inside a function is limited to that function and likewise for variables outside a function.

What confused me about the object creation was that in Java I create the object in the Main class. Seeing the object being created at the end of the file threw me off for a while.

Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
Allthough squid has 1 object, it still asks object to http server markraem Linux - Server 0 10-08-2007 11:40 AM
Shared object library that uses another shared object library Nightfox Programming 1 02-15-2006 10:00 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration