Solved

Create a php object

Posted on 2012-03-13
4
398 Views
Last Modified: 2012-03-13
Experts,

I'd like a class object that populates itself when I supply a parameter.  

EG pseudocode:

---
class User

agent = new User(1001)
---

How do I make the object constructor so that if the parameter is supplied the object populates with the requested values or if not, just creates a new object?

Capt
0
Comment
Question by:Glen Gibb
4 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 37713649
MAybe something like this

class test {
 
     function __construct( $userId = '' ) {

          if ( $userId == '' )  {
               // no data passed
               ... code to initialise a blank class
          }
          else {
               .... code to populate class
          }
     }
}


The key to this is to give the parameter a default value that is not used in 'real life'. So if $userID is an integer then a blank string is a acceptable 'nothing passed' indicator. If the user ID could be a string then I would use a non-valid value like *none

class test {
 
     function __construct( $userId = '*none' ) {

          if ( $userId == '*none' )  {
               // no data passed
               ... code to initialise a blank class
          }
          else {
               .... code to populate class
          }
     }
}
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37714000
No points for this, please, but the required reading begins here:
http://php.net/manual/en/language.oop5.php
0
 
LVL 10

Accepted Solution

by:
Derokorian earned 250 total points
ID: 37715156
The key to this is to give the parameter a default value that is not used in 'real life'. So if $userID is an integer then a blank string is a acceptable 'nothing passed' indicator. If the user ID could be a string then I would use a non-valid value like *none
I prefer to use NULL and is_null for all non-required parameters. Making up nonsensical strings always seemed like poor form to me because you end up checking for different values in different functions as opposed to all functions following a single style like such:
function foo($bar = NULL) {
   if( is_null($bar) ) {
       // No argument passed
   } else {
       // argument supplied, validate its contents and use it here
   }
}

Open in new window

Of course that's just my personal preference.
0
 

Author Closing Comment

by:Glen Gibb
ID: 37715751
Thank you, all experts.  Just what I needed.

Ray, thanks for the reference.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what do I need to host my own web sites? 13 51
MVC - procedural PHP 10 37
Uploading a CSV Data Import via PHP & MySql 3 32
$_SERVER Variable question 31 29
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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