Jump to content

Recommended Posts

if it's inside a class, you'll need an instance of the class to use it. and if you're going that far, you might as well make it a class variable, callable from outside, sort of like:

 

$some_object = new someClass();
$some_object->$class_variable = 10;

// later
$c_var = $some_object->$class_variable;

How do I make a variable in one class function noticeable to other class functions?

 

Are you talking about within the same class? Your question is pretty unclear, but an example (within the same class) would be....

 

<?php

  class foo {

    private $a;

    function x() {
      $this->a = 'hi!';
    }

    function y() {
      echo $this->a;
    }

  }

  $foo = new foo;
  $foo->x();
  $foo->y();

?>

Only to be used inside the class and has a class scope(can be used by all functions within the class). I am wondering...

Is this:

<?php
$this->a = 'hi!';
?>

How you set variables within a class? Or can you use something like this:

<?php
private $a = 'hi!';
?>

Only to be used inside the class and has a class scope(can be used by all functions within the class). I am wondering...

Is this:

<?php
$this->a = 'hi!';
?>

How you set variables within a class? Or can you use something like this:

<?php
private $a = 'hi!';
?>

 

You can preinitialize private variables, so, yes, the following will work:

<?php
   private $foo = "Hi, I'm a private variable!";
?>

 

You can access/assign to private variables from within the class by using the 'this' keyword, like normal.  So:

<?php
   private $foo;

   public function setFoo($string)
   {
      $this->foo = $string;
   }

   public function printFoo()
   {
      if($this->foo)
      {
         echo $this->foo;
      }
      else
      {
         echo "Foo not set";
      }
   }
?>

So when dealing with variables within classes you have to use $this?

 

If they're part of the class/object itself, yes.  The 'this' keyword refers to the current instantiated object.  So:

<?php
   class Example
   {
      private $myGoodies;

      public function __construct($stringVal = null) //object constructor.  $stringVal has a default value of null.
      {
         $this->myGoodies = $stringVal;
      }

      public function setMyGoodies($stringVal) //function to explicitly set $myGoodies to $stringVal if not done so with the constructor
      {
         $this->myGoodies = $stringVal;
      }

      public function getMyGoodies()
      {
         return $this->myGoodies;
      }
   }

   $example1 = new Example();
   $example1->setMyGoodies("This is example1");

   $example2 = newExample("This is example2");

   $example1_output = $example1->getMyGoodies();
   $example2_output = $example2->getMyGoodies();

   echo "Example 1: $example1_output -- Example 2: $example2_output";

   /*
   Should print:
   Example 1: This is example1 -- Example 2: This is example2
   */
?>

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.