?
Solved

3 calling: this, parent, self

Posted on 2013-12-01
8
Medium Priority
?
185 Views
Last Modified: 2013-12-05
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
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 39688927
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
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1000 total points
ID: 39689991
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
 

Author Comment

by:rgb192
ID: 39692532
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
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!

 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39692584
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
 

Author Comment

by:rgb192
ID: 39692624
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39692795
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
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39693011
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
 

Author Closing Comment

by:rgb192
ID: 39700253
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question