using a PHP class
Moderator: General Moderators
using a PHP class
OK I have been scrutinizing some code I got from here and I starting to understand PHP better finally.
given a bunch of functions in a class, do I need to create a container for the class like C++ or is it already there
then how about calling a member function, in C++ its usually class::member
given a bunch of functions in a class, do I need to create a container for the class like C++ or is it already there
then how about calling a member function, in C++ its usually class::member
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
My site is powered by LAMP
Re: using a PHP class
Given functions in a class do you need a class for them? Want to try asking that again?Vegan wrote:given a bunch of functions in a class, do I need to create a container for the class like C++ or is it already there
Vegan wrote:then how about calling a member function, in C++ its usually class::member
Code: Select all
// instance methods
$this->method() // inside the class
$object->method() // outside the class
// static methods
self::method() // inside the class
parent::method() // inside the class but calling the parent's implementation
Class::method() // inside or outside the class- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: using a PHP class
The class construct is the container. In PHP:Vegan wrote:given a bunch of functions in a class, do I need to create a container for the class like C++ or is it already there
Code: Select all
class Foo
{
protected $bar = 'Hello';
public function show($baz)
{
echo "property bar={$this->bar}, parameter baz=$baz";
}
}
$foo = new Foo();
$foo->show('world');(#10850)
Re: using a PHP class
Code: Select all
class AdFactory {
protected $ads = array(
// used, HTML
array(false, "insert ad 1 here"),
array(false, "insert ad 2 here")
);
protected $uniqueremaining;
public function __construct() {
$this->uniqueremaining = count($this->ads);
}
public function addAd($html) {
$this->ads[] = array(false, $html);
$this->uniqueremaining++;
}
public function getAd() {
return $this->ads[mt_rand(0, count($this->ads) - 1)][1];
}
public function getUniqueAd($fallback = false) {
if ($this->uniqueremaining == 0) {
return ($fallback ? $this->getAd() : false);
}
$n = mt_rand(1, $this->uniqueremaining);
foreach ($this->ads as $key => $ad) {
if (!$ad[0] && --$n == 0) {
$this->ads[$key][0] = true;
return $ad[1];
}
}
}
}
$a = new AdFactory;
$a = getAd();
echo $a
etc
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
My site is powered by LAMP
Re: using a PHP class
Yes (except that you reuse $a). If you want two ads then call getAd() twice but on the same factory.
[edit] Wow. Yeah. Have to use $factory. Oops.
Code: Select all
$factory = new AdFactory;
$a = getAd();
echo $a;
$b = getAd();
echo $b;Code: Select all
$factory = new AdFactory;
$a = $factory->getAd();
echo $a;
$b = $factory->getAd();
echo $b;
Last edited by requinix on Mon Jul 16, 2012 10:32 pm, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: using a PHP class
Actually it should be:
It seems a little procedural for me. Not sure why its a Factory. Seems more like a Gateway to a data table full of ads. Maybe something more like:
Code: Select all
$factory = new AdFactory;
$ad = $factory->getAd();
echo $adCode: Select all
class AdGateway
{
public function find($id);
public function findRandom();
public function findUnique();(#10850)
Re: using a PHP class
I wrote it in OP's previous thread but wasn't quite sure what the requirements or usages would be. So I just guessed a generic factory.Christopher wrote:It seems a little procedural for me. Not sure why its a Factory. Seems more like a Gateway to a data table full of ads.
Re: using a PHP class
generic is always better as its clearer when a different use is envisioned
how about showing images of a food item on my vegan site, selecting from one of 35 pics of different potatoes for example.
I am still learning more about PHP and how I can leverage it to make my web sites better
how about showing images of a food item on my vegan site, selecting from one of 35 pics of different potatoes for example.
I am still learning more about PHP and how I can leverage it to make my web sites better
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
My site is powered by LAMP
Re: using a PHP class
Need to know more. Is a list of pictures with radio buttons not enough?Vegan wrote:how about showing images of a food item on my vegan site, selecting from one of 35 pics of different potatoes for example.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: using a PHP class
The actual task would be selecting one of 35 strings. So a class could do that. Another datasource class could get the list of image names from some datasource. And finally a helper class could generate a <img> tag using the selected image name.Vegan wrote:how about showing images of a food item on my vegan site, selecting from one of 35 pics of different potatoes for example.
(#10850)
Re: using a PHP class
I could cobble up some code to generate the <img> tags easy enough.
It definitely has caused me to come up with a lot of ideas
how about joke of the day etc
It definitely has caused me to come up with a lot of ideas
how about joke of the day etc
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
My site is powered by LAMP