Solved

Create a php object

Posted on 2012-03-13
4
395 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, all experts.  Just what I needed.

Ray, thanks for the reference.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

11 Experts available now in Live!

Get 1:1 Help Now