Solved

Create a php object

Posted on 2012-03-13
4
396 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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

15 Experts available now in Live!

Get 1:1 Help Now