PHP oop goodness :-)

Posted on 2007-10-16
Last Modified: 2012-05-05
Hi there,

Just getting my hands on some PHP5 oop goodness...
now just writing up a class that will handle a user.

now I can do
class user
  private $id   = -1;
  private $fname = '';

then do a function load_user($id)
select user from database and use mysql_fetch_assoc
$user_data = mysql_fetch_assoc($result);
Foreach ($user_data AS $key->$var)
  $this->$key = $var;

however, what happens if I don't want to declear all those private vars at the start of the class...
can I do
Foreach ($user_data AS $key->$var)
  private($this->$key = $var);
or some other way of telling PHP5 that this new var is to be private. Because by default it's public.

Just trying to make it as clean cut and readable as possible....

Question by:wildzero
    LVL 3

    Accepted Solution

    Every programming book I've read advocates declaring all your vars at the beginning, making them private, and giving access to vars only via getters & setters. I declare any properties at the beginning of the class, but it doesn't necessarily matter where you declare them, that's your preference. The only thing you can't do is use the private declaration as if it were a  function ie "private($this->key = $var)" you'd have to separate that out, declare the var first, then assign the value to it.
    I don't know if you just typed that code up real quick or if it's actually in use, but a couple things to watch out for:
    when using $this->somevar, you don't put a $ in front of the var name your referencing.  Also when using foreach, you should have to use => in the statement instead of ->, I know my debugger errors out if I type it that way.

    Hopefully that helps.
    LVL 48

    Assisted Solution

    If you want "generic" private attributes, why not store all those attributes in an private array like:

    class user
          private $_data = array();

    Then assign the $user_data to $_data

    $this->_data = $user_data;

    access the attributes with $this->_data['id'] instead of $this->id

    defining attributes of a class on the fly makes no real sense in terms of OOP.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now