rgb192
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','mar tha'
pdo call returning a list
select * from users
'bob','james','sarah','mar tha'
want to call a method that takes one paramater (either from pdo select statment or hardcoded list of usernames)
$lastNames=new ShowData;
$lastNames->showData($user names);
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
based upon an array
hardcode list of usernames
'bob','james','sarah','mar
pdo call returning a list
select * from users
'bob','james','sarah','mar
want to call a method that takes one paramater (either from pdo select statment or hardcoded list of usernames)
$lastNames=new ShowData;
$lastNames->showData($user
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
What is the structure of the users table?
ASKER
username varchar(20)
firstname varchar(20)
lastname 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
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
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?
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?
ASKER
$lastNames=new ShowData;
$lastNames->showData($user names);
would be
$lastNames=new ShowData;
$lastNames->showData('sele ct 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 ($hardcode darray );
}elseif pdo{
$lastNames=new ShowData;
$lastNames->showData(query ('select userNames from users') );
}
there must be a better way to write this
$lastNames->showData($user
would be
$lastNames=new ShowData;
$lastNames->showData('sele
but there is a pdo call
$lastNames=new ShowData;
$lastNames->showData(query
and what if the data is coming from hardcoded array
if hardcoded array{
$lastNames=new ShowData;
$lastNames->showData(query
}elseif pdo{
$lastNames=new ShowData;
$lastNames->showData(query
}
there must be a better way to write this
ASKER
Is there a better way to avoid the if else.
This does not make sense:
$lastNames=new ShowData;
$lastNames->showData($user names);
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?
$lastNames=new ShowData;
$lastNames->showData($user
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?
ASKER
$lastNames=new ShowData;
echo $lastNames->showData($user names);
echo $lastNames->showData($user
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Okay a foreach would be this pattern
Thanks