[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Create a php object

Posted on 2012-03-13
4
Medium Priority
?
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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