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
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
What is the structure of the users table?
0
rgb192Author Commented:
username varchar(20)
firstname varchar(20)
lastname varchar(20)
0
Ray PaseurCommented:
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rgb192Author Commented:
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
0
Ray PaseurCommented:
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?
0
rgb192Author Commented:
$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
0
rgb192Author Commented:
Is there a better way to avoid the if else.
0
Ray PaseurCommented:
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?
0
rgb192Author Commented:
$lastNames=new ShowData;
echo $lastNames->showData($usernames);
0
Ray PaseurCommented:
I don't know if I am even close to answering the question.  Probably because this is not a question that would come up in application development.  Here is how the design would work.

Each person would be represented by an object.  

The person objects would be the rows of results created by a query.  

The query would retrieve the first and last names from the SQL table, and would present the results set in the form of rows, with an array of person objects (if PDO) or a resource that returns the rows of person objects one at a time, via a while() iterator (if MySQLi).

When you use mysqli::fetch_object() you can give MySQLi the name of a class to instantiate as it retrieves the rows; it does not have to be just stdClass.  

In any case you will have a 1:1 relationship between persons in the data base and the person objects created in the PHP script.  If you wanted to find the last name for any single person, you would access the last name property of the person object.

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.