using a PHP class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

using a PHP class

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using a PHP class

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: using a PHP class

Post 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
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: using a PHP class

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using a PHP class

Post 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;
Last edited by requinix on Mon Jul 16, 2012 10:32 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: using a PHP class

Post 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();
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using a PHP class

Post 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.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: using a PHP class

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using a PHP class

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: using a PHP class

Post 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.
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: using a PHP class

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply