Solved

3 calling: this, parent, self

Posted on 2013-12-01
8
163 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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 34

Accepted Solution

by:
gr8gonzo earned 250 total points
Comment Utility
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
Comment Utility
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
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:rgb192
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 34

Expert Comment

by:gr8gonzo
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now