Asked  7 Months ago    Answers:  5   Viewed   29 times

I'm just starting with Object Oriented PHP and I have the following issue:

I have a class that contains a function that contains a certain script. I need to call a variable located in that script within another function further down the same class.

For example:

class helloWorld {

function sayHello() {
     echo "Hello";
     $var = "World";
}

function sayWorld() {
     echo $var;
}


}

in the above example I want to call $var which is a variable that was defined inside a previous function. This doesn't work though, so how can I do this?

 Answers

88

you should create the var in the class, not in the function, because when the function end the variable will be unset (due to function termination)...

class helloWorld {

private $var;

function sayHello() {
     echo "Hello";
     $this->var = "World";
}

function sayWorld() {
     echo $this->var;
}


}
?>

If you declare the Variable as public, it's accessible directly by all the others classes, whereas if you declare the variable as private, it's accessible only in the same class..

<?php
 Class First {
  private $a;
  public $b;

  public function create(){
    $this->a=1; //no problem
    $thia->b=2; //no problem
  }

  public function geta(){
    return $this->a;
  }
  private function getb(){
    return $this->b;
  }
 }

 Class Second{

  function test(){
    $a=new First; //create object $a that is a First Class.
    $a->create(); // call the public function create..
    echo $a->b; //ok in the class the var is public and it's accessible by everywhere
    echo $a->a; //problem in hte class the var is private
    echo $a->geta(); //ok the A value from class is get through the public function, the value $a in the class is not dicrectly accessible
    echo $a->getb(); //error the getb function is private and it's accessible only from inside the class
  }
}
?>
Wednesday, March 31, 2021
 
leebriggs
answered 7 Months ago
35

use codeigniter's tiny feature called Flashdata which allows you to have temorarily store data between requests.

so your code would be

function send data{
$this->session->set_flashdata('recordid', $recordid);
}

function success{
 $recordid =  $this->session->flashdata('recordid');
}

got it ?

Saturday, May 29, 2021
 
phpmeh
answered 5 Months ago
98

It should be something like this.

info.php

class Connection {
   // these two variable should be declared within the class.
   protected $db; // to be able to access these variables from a diff class
   protected $db1; // either their scope should be "protected" or define a getter method.

   public function __construct() {
      $this->connect();
   }

   private function connect() {
       $this->db = 'hello world';
       $this->db1 = 'hi';
   }
}

prd.php

require_once 'info.php';

// you are accessing the Connection class in static scope
// which is not the case here.
class prdinfo extends Connection {
   public function __construct() {
       // initialize the parent class
       // which in turn sets the variables.
       parent::__construct();
   }

   public function productId() {
        echo $this->db;
        echo $this->db1;
   }
}


$prd = new prdinfo ();
$prd->productId ();

This is a basic demonstration. Modify it as per your needs. More here - http://www.php.net/manual/en/language.oop5.php

Saturday, May 29, 2021
 
Trott
answered 5 Months ago
15
function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
}
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
}
Saturday, May 29, 2021
 
RenegadeAndy
answered 5 Months ago
22

add these code into the top of your script

@set_magic_quotes_runtime(false);
ini_set('magic_quotes_runtime', 0);
Wednesday, August 18, 2021
 
Warrior
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :