?
Solved

Create a php object

Posted on 2012-03-13
4
Medium Priority
?
409 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 1000 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 111

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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
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 count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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