• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

3 calling: this, parent, self

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28305101.html#a39685489

If you want to do three things, you probably want to write three separate pieces of code.  Class properties are either static or not.  They aren't both.


I am looking for the 3 pieces of code that solve this problem because I was told that I can not do this with 1 piece of code
0
rgb192
Asked:
rgb192
  • 3
  • 3
  • 2
2 Solutions
 
Ray PaseurCommented:
Please see: http://www.laprbass.com/RAY_temp_rgb192.php

Outputs:
int(3)
int(5)
object(Instantiated_Thing)#1 (1) {
  ["data"]=>
  int(12)
}

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28307551.html
// SEE http://php.net/manual/en/language.oop5.static.php


Class Static_Thing_Parent
{
    public static $data = 3;
}
Class Static_Thing_Child extends Static_Thing_Parent
{
    public static $data = 5;
}
Class Instantiated_Thing
{
    public function __construct($x)
    {
        $this->data = $x;
    }
}

$xyz = Static_Thing_Parent::$data;
var_dump($xyz);

$xyz = Static_Thing_Child::$data;
var_dump($xyz);

$xyz = new Instantiated_Thing(12);
var_dump($xyz);

Open in new window

0
 
gr8gonzoConsultantCommented:
Just one additional thought - Ray's example is correct, but you CAN have a static property on an instantiated CLASS.

Static properties belong to the DEFINITION of the class. (All cars have wheels.)

Non-static properties belong to the INSTANCE of the class. (-My- car is blue and -your- car is red, but they are still cars, and so they both have wheels.)

Imagine you are walking along and you are eating an apple. Someone comes along and asks you, "Where did you get that apple?" They are asking about the instance of the apple - where did you buy that instance of an apple? That would be a non-static property of your apple.

Then they ask you, "What do apples taste like?" That is a static property of the apple class, because it is a description of what ALL apples taste like, and it would apply even if he went and bought one for himself.

Take a look at the below exercise:
<?php
error_reporting(E_ALL);

class Fruit
{
  public static $tastes_like = "Delicious!";
  public static $edible = true;
}

class Apple extends Fruit
{
  public static $tastes_like = "Delicious!";
  public $bought_from;
}

class FruitStore
{
  // Our store's name
  public $name = "";

  // All new stores start with 100 apples
  public $apples_in_stock = 100;
    
  // Build our store
  public function __construct($name)
  {
  	$this->name = $name;
  }
  
  // Allow people to buy apples
  public function BuyApple()
  {
     // Decrease our number of apples in stock
     $this->apples_in_stock--;

     // Prepare our apple
     $purchased_apple = new Apple();

     // Tell our apple where it came from
     $purchased_apple->bought_from = $this;
     
     // Give it to the buyer
     return $purchased_apple;
  }
}

// Build some stores
$RaysApples = new FruitStore("Ray's Apples");
$GonzosApples = new FruitStore("Gonzo's Apples");

// Buy an apple from Gonzo's store!
$my_apple = $GonzosApples->BuyApple();

// What does my apple taste like?
echo "My new apple tastes: " . $my_apple::$tastes_like . "\n";

// Because $tastes_like is a static property, it tastes like all other apples: Delicious!
echo "All apples taste: " . Apple::$tastes_like . "\n";

// What if suddenly all apples went rotten?
Apple::$tastes_like = "Awful!";

// NOW what does my apple taste like?
echo "My new apple tastes: " . $my_apple::$tastes_like . "\n";

// Because $tastes_like is a static property, it tastes like all other apples: Awful!
echo "All apples taste: " . Apple::$tastes_like . "\n";

// Where did MY apple come from?
echo "My apple came from " . $my_apple->bought_from->name . "\n";

// What are our store inventories?
echo $RaysApples->name . " has " . $RaysApples->apples_in_stock . " apples in stock!\n";
echo $GonzosApples->name . " has " . $GonzosApples->apples_in_stock . " apples in stock!\n";

// Let's buy something from Ray:
$your_apple = $RaysApples->BuyApple();

// Where did YOUR apple come from?
echo "Your apple came from " . $your_apple->bought_from->name . ", which now has " . $your_apple->bought_from->apples_in_stock . " apples left in stock!\n";

// Since Apple is a Fruit, it gets the Fruit's static properties, too:
echo "Apples are " . (Apple::$edible ? "edible" : "inedible") . "\n";

// What if all fruit was inedible?
Fruit::$edible = false;
echo "Apples are " . (Apple::$edible ? "edible" : "inedible") . "\n";

// Children's static properties override their parents' static properties:
echo "Fruit is " . Fruit::$tastes_like . ", but Apples are: " . Apple::$tastes_like . "\n";

Open in new window

0
 
rgb192Author Commented:
In Ray's example
I do not understand why Child was used
Line 14,29

because variable can be ::child

gr8gonzo
static variable was $variable1::$variable2 which generated error and there was no my_apple class but there is a $my_apple instance.  I do not understand

This ran, but is this correct

<?php
error_reporting(E_ALL);

class Fruit
{
  public static $tastes_like = "Delicious!";
  public static $edible = true;
}

class Apple extends Fruit
{
  public static $tastes_like = "Delicious!";
  public $bought_from;
}

class FruitStore
{
  // Our store's name
  public $name = "";

  // All new stores start with 100 apples
  public $apples_in_stock = 100;
    
  // Build our store
  public function __construct($name)
  {
    $this->name = $name;
  }
  
  // Allow people to buy apples
  public function BuyApple()
  {
     // Decrease our number of apples in stock
     $this->apples_in_stock--;

     // Prepare our apple
     $purchased_apple = new Apple();

     // Tell our apple where it came from
     $purchased_apple->bought_from = $this;
     
     // Give it to the buyer
     return $purchased_apple;
  }
}

// Build some stores
$RaysApples = new FruitStore("Ray's Apples");
$GonzosApples = new FruitStore("Gonzo's Apples");

// Buy an apple from Gonzo's store!
$my_apple = $GonzosApples->BuyApple();

// What does my apple taste like?
echo "My new apple tastes: " . Apple::$tastes_like . "\n";

// Because $tastes_like is a static property, it tastes like all other apples: Delicious!
echo "All apples taste: " . Apple::$tastes_like . "\n";

// What if suddenly all apples went rotten?
Apple::$tastes_like = "Awful!";

// NOW what does my apple taste like?
echo "My new apple tastes: " . Apple::$tastes_like . "\n";

// Because $tastes_like is a static property, it tastes like all other apples: Awful!
echo "All apples taste: " . Apple::$tastes_like . "\n";

// Where did MY apple come from?
echo "My apple came from " . $my_apple->bought_from->name . "\n";

// What are our store inventories?
echo $RaysApples->name . " has " . $RaysApples->apples_in_stock . " apples in stock!\n";
echo $GonzosApples->name . " has " . $GonzosApples->apples_in_stock . " apples in stock!\n";

// Let's buy something from Ray:
$your_apple = $RaysApples->BuyApple();

// Where did YOUR apple come from?
echo "Your apple came from " . $your_apple->bought_from->name . ", which now has " . $your_apple->bought_from->apples_in_stock . " apples left in stock!\n";

// Since Apple is a Fruit, it gets the Fruit's static properties, too:
echo "Apples are " . (Apple::$edible ? "edible" : "inedible") . "\n";

// What if all fruit was inedible?
Fruit::$edible = false;
echo "Apples are " . (Apple::$edible ? "edible" : "inedible") . "\n";

// Children's static properties override their parents' static properties:
echo "Fruit is " . Fruit::$tastes_like . ", but Apples are: " . Apple::$tastes_like . "\n";

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gr8gonzoConsultantCommented:
Sorry - the $instance_of_apple_class::$some_variable syntax can generate an error on older PHP versions, so it depends on what version you're on. In newer versions, PHP can look at $instance_of_apple_class and figure out that it's the Apple class and then call Apple::$some_variable_syntax.

I was trying to show that PHP was doing this with $my_apple (in newer versions). It wasn't looking for a class called $my_apple, but instead it was looking up which class $my_apple was using, and then running the same code as Apple::$tastes_like.

So you're still understanding the point, which is - it does the same thing. The PROPER and more compatible syntax is simply Class::$static_variable.
0
 
rgb192Author Commented:
In newer versions, PHP can look at $instance_of_apple_class and figure out that it's the Apple class and then call Apple::$some_variable_syntax.

Is this an instance variable using a static variable

the results of the 2 codes are the same, so does that mean that the codes are the same

my edit of your code in older php
My new apple tastes: Delicious! All apples taste: Delicious! My new apple tastes: Awful! All apples taste: Awful! My apple came from Gonzo's Apples Ray's Apples has 100 apples in stock! Gonzo's Apples has 99 apples in stock! Your apple came from Ray's Apples, which now has 99 apples left in stock! Apples are edible Apples are inedible Fruit is Delicious!, but Apples are: Awful!

your code in newer php:
My new apple tastes: Delicious! All apples taste: Delicious! My new apple tastes: Awful! All apples taste: Awful! My apple came from Gonzo's Apples Ray's Apples has 100 apples in stock! Gonzo's Apples has 99 apples in stock! Your apple came from Ray's Apples, which now has 99 apples left in stock! Apples are edible Apples are inedible Fruit is Delicious!, but Apples are: Awful!




I do not know what your version of code does because I test on nusphere phpED ide and run code through a step by step line by line debugger
0
 
Ray PaseurCommented:
because variable can be ::child
I don't think so.  The double colon scope resolution operator is used to name a property or method like this:

className::$propertyName
className::methodName()
Is this an instance variable using a static variable
The terminology may be a little off.  Let me try to put some of this together.  A class is like a blueprint.  It defines the structure of an object, but it is not an object.  An object is created when you use the new keyword giving the name of the class.  The object is an instance of the class.  The process of creating the object with the new keyword is called instantiation.

An instance variable is a property of the object, and each object instance of the class can have different values in the property.  A static variable would have only one value, no matter how many instances of the class have been created.

http://en.wikipedia.org/wiki/Instance_variable
http://en.wikipedia.org/wiki/Static_member_variable
0
 
gr8gonzoConsultantCommented:
does that mean that the codes are the same

Yes, it does. The error you got before was just because there was some code in there that required a newer version of PHP than what you used to run it.

The results are the same, though.
0
 
rgb192Author Commented:
In newer versions, PHP can look at $instance_of_apple_class and figure out that it's the Apple class and then call Apple::$some_variable_syntax.

I was trying to show that PHP was doing this with $my_apple (in newer versions). It wasn't looking for a class called $my_apple, but instead it was looking up which class $my_apple was using, and then running the same code as Apple::$tastes_like.

So you're still understanding the point, which is - it does the same thing. The PROPER and more compatible syntax is simply Class::$static_variable.


thanks
I will work on updating php on all my environments to 5.4
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now