Re: Re: Programming general question
| From: | Usamah M. Ali | Date: | Wed, 28 Jan 2009 22:38:49 +0000 |
| Subject: | Re: Re: Programming general question | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287331@lists.php.net to get a copy of this message | ||
On Wed, Jan 28, 2009 at 11:07 PM, Terion Miller <webdev.terion@gmail.com> wrote:
> On Wed, Jan 28, 2009 at 3:54 AM, Ondrej Kulaty <kopyto911@gmail.com> wrote:
>> Thanks for all the information, I was asking because I inherited thousands
> of lines of code that had used mysql_fetch_object(), yet I kept getting so
> many errors so I starting playing with changing them to mysql_fetch_assoc()
> and things seem to work better, so I was wonder why sometimes data from a db
> field can be used as an object or array, the arrays, objects, classes still
> confuses me..oh add functions to that, is there a hiearchy?
>
If an object is not an ACTUAL class instance, then it's no more than a
syntactic sugar array. For example:
$user = new stdClass(); // General class name, used internally by PHP
$user->username = 'John Doe',
$user->email = 'john_doe@example.com';
$user->password = md5('some_hard_or_easy_password_string');
...
is almost typical to:
$user = array();
$user['username'] = 'John Doe';
$user['email'] = 'john_doe@example.com';
$user['password'] = md5('some_hard_or_easy_password_string');
...
Many programmers particularly use objects for retrieving database
results because they require less verbose than arrays:
$result->username is just easier and less error-prone than $result['username'].
Using objects as class instances is totally different than using
arrays as many repliers have said. They're usually used, objects that
is, within OOP (Object-Oriented Programming), which relies on the idea
of thinking of programming in resemblance to real-world things
(objects). Thinking in this paradigm is, again, totally different than
traditional procedural programming, and requires a hard shift in the
way you think about programming in general. I suggest consulting the
manual sections on OOP (specially for PHP5), or other basic tutorials
or books on this huge subject.
Regards,
Usamah