Page 1 of 1
using a PHP class
Posted: Thu Jul 12, 2012 5:43 pm
by Vegan
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
Re: using a PHP class
Posted: Thu Jul 12, 2012 7:10 pm
by requinix
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
Given functions
in a class do you need a class for them? Want to try asking that again?
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
Re: using a PHP class
Posted: Thu Jul 12, 2012 11:10 pm
by Christopher
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
The class construct is the container. In PHP:
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');
See the manual for Classes and Objects:
http://de.php.net/manual/en/language.oop5.basic.php
Re: using a PHP class
Posted: Mon Jul 16, 2012 5:58 pm
by Vegan
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];
}
}
}
}
so if I want to get an ad
$a = new AdFactory;
$a = getAd();
echo $a
etc
Re: using a PHP class
Posted: Mon Jul 16, 2012 7:35 pm
by requinix
Yes (except that you reuse $a). If you want two ads then call getAd() twice but on the same factory.
Code: Select all
$factory = new AdFactory;
$a = getAd();
echo $a;
$b = getAd();
echo $b;
[edit] Wow. Yeah. Have to use $factory. Oops.
Code: Select all
$factory = new AdFactory;
$a = $factory->getAd();
echo $a;
$b = $factory->getAd();
echo $b;
Re: using a PHP class
Posted: Mon Jul 16, 2012 10:27 pm
by Christopher
Actually it should be:
Code: Select all
$factory = new AdFactory;
$ad = $factory->getAd();
echo $ad
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
class AdGateway
{
public function find($id);
public function findRandom();
public function findUnique();
Re: using a PHP class
Posted: Mon Jul 16, 2012 10:34 pm
by requinix
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.
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.
Re: using a PHP class
Posted: Thu Aug 30, 2012 3:24 pm
by Vegan
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
Re: using a PHP class
Posted: Thu Aug 30, 2012 4:41 pm
by requinix
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.
Need to know more. Is a list of pictures with radio buttons not enough?
Re: using a PHP class
Posted: Thu Aug 30, 2012 6:32 pm
by Christopher
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.
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.
Re: using a PHP class
Posted: Mon Sep 03, 2012 7:08 pm
by Vegan
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