Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

object oriented method to echo lastNames

using object oriented programming, please show me how to echo lastNames
based upon an array



hardcode list of usernames
'bob','james','sarah','martha'

pdo call returning a list
select * from users
'bob','james','sarah','martha'


want to call a method that takes one paramater (either from pdo select statment or hardcoded list of usernames)

$lastNames=new ShowData;
$lastNames->showData($usernames);

foreach ($lastNames as $lastName){
  echo '<br>lastName is: '.$lastName;
}


class showData{
  public function lastName(array of $userName){
    return lastName;
  }
}


final output
smith
davis
evans
jones
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What is the structure of the users table?
Avatar of rgb192

ASKER

username varchar(20)
firstname varchar(20)
lastname varchar(20)
There's nothing here that argues for an OOP design at all.  You can write OOP code with functions and fetch_object() calls, but this is a simple SELECT... WHERE query
Avatar of rgb192

ASKER

maybe it is a factory design pattern

because parameters are
hardcoded array
or
select statement that has been through the pdo function and appears to be an array

and the output is the same
I'm still not getting the feeling that the OO Factory pattern is part of this conversation.
http://en.wikipedia.org/wiki/Factory_method_pattern

Maybe at a very theoretical level, you could argue that an abstraction layer would choose whether to use the hardcoded information or the data base.  Perhaps the constructor could try to find the data base and load the information object from the data base, but if the data base is not there, it could load the information object from the hardcoded array?  Then any subsequent request for the object would choose the data that was built by the "factory," without needing to know where the data originated.  Does that sound like what you want?
Avatar of rgb192

ASKER

$lastNames=new ShowData;
$lastNames->showData($usernames);


would be

$lastNames=new ShowData;
$lastNames->showData('select userNames from users' );

but there is a pdo call

$lastNames=new ShowData;
$lastNames->showData(query('select userNames from users') );

and what if the data is coming from hardcoded array

if hardcoded array{
$lastNames=new ShowData;
$lastNames->showData(query($hardcodedarray );

}elseif pdo{
$lastNames=new ShowData;
$lastNames->showData(query('select userNames from users') );
}

there must be a better way to write this
Avatar of rgb192

ASKER

Is there a better way to avoid the if else.
This does not make sense:

$lastNames=new ShowData;
$lastNames->showData($usernames);



What we have there is a two line script.  The first line creates an object instance of the ShowData class and assigns to the object a variable pointer $lastNames.

The second line calls the showData() method on the $lastNames object, but it does not retrieve any data from the object.  A tree falls in the forest, but there is nobody there to hear the sound.  So for anything to matter, there needs to be more code, right?
Avatar of rgb192

ASKER

$lastNames=new ShowData;
echo $lastNames->showData($usernames);
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

If you wanted to find the last names for all of the persons, you would use an iterator like foreach() to iterate over the array of person objects.  Inside the foreach() loop you would access the last name of the person object.

Okay a foreach would be this pattern

Thanks