How to access function value in another function in PHP Classes - php

How can i return a variable value from one function and pass it to another function in a class and access it ?, im new to php class.
here is the code i have tried
<?php
class BioTool{
public function one(){
$lol =[2,3,5]; print_r($lol);
}
public function two(){
$newarr=$this->one(); //this only return the array but i can't access it, check below.
print_r($newarr[0]); //not working
}
}
$biotool=new BioTool();
$biotool->two();

Thanks to #ADyson, i had to return from the function instead of echo.
<?php
class BioTool{
public function one(){
print_r($lol);
return $lol =[2,3,5];
}
public function two(){
$newarr=$this->one();
print_r($newarr[0]); // working
}
}
$biotool=new BioTool();
$biotool->two();

Related

PHP function working differently for return and echo. Why?

By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();

Get values from vars inside class across other functions

class dir_exam
{
public $db_ruta;
function __construct($db_ruta)
{
$this->db_ruta=$db_ruta;
}
function veritas()
{
$aa="ok";
$xx="ok2";
return $aa;
return $xx;
}
function create_d()
{
$r=$this->veritas();
echo $r->$aa;
echo $r->$xx;
}
}
I have this class and i try execute funtion veritas inside function create_d, but i want show the value from function veritas as individual values, showing value in create_d for $aa and $xx, when execute finally the class
<?php
$a=new dir_exam("db_p");
echo $a->create_d();
?>
But i can´t get this finally, i don´t know if it´s not possible or what, this it´s my question, thank´s in advanced
You can't have 2 or more returns in a function.
For you use the vars $aa and $xx like OOP, you must create the 2 var in the class
class dir_exam
{
public $db_ruta;
public $aa; // <--
public $xx; // <--
}
After, you need change the function veritas to pass the value for your attributes
function veritas()
{
$this->aa="ok";
$this->xx="ok2";
}
Now in your function you can call like that:
function create_d()
{
$this->veritas();
echo $this->aa;
echo $this->xx;
}

Return array variable in PHP

I have a function and I'd like to return a variable to another function.
Can I return the array variable so I can use the variable at other function?
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
When I try to use $araydatamdr in another function, it became 0.
Am I missing something?
You can achieve this by calling function and setting its return value to another variable.
Method 1 :
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
public function test_func() {
$araydatamdr = $this->update_mdr_pameran();
var_dump($araydatamdr);
}
}
Or you can also set $araydatamdr to $this reference.
Method 2 :
class Test extends CI_Controller {
public $araydatamdr;
public function __construct() {
parent::__construct();
$this->araydatamdr = [];
}
public function update_mdr_pameran() {
$this->config->set_item('compress_output', FALSE);
$this->araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$this->araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$this->araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
}
public function test_func() {
$this->update_mdr_pameran();
var_dump($this->araydatamdr);
}
}
Cross out the echo $araydatamdr; Arrays can be printed using var_dump or print_r. Also you can return an array in php but personally i prefer to json_encode it first so i return a json as the output of my function something like:
return json_encode($araydatamdr);
Then it's a simple function call.
I don't know your project structure but i am giving general guidance. Apart from that i don't see anything else that could block your function.
I edit my post because i saw the issue is to call the function. There are 2 ways depending where you call it. If the function is in the same class as the other function you want to call it you simple go for :
$result=$this->update_mdr_pameran();
I see that your function has no arguments so you don't need to set any. If it's in another file:
1) include your php file at top like :
require 'myphpclass.php';
*tip make sure your path is right.
2) Create a new class object and then call the function like :
$class= new myClass();
$result=$class->update_mdr_pameran();

global variable can't accessible inside the function

I need to access the global variable from another function. First I have assigned the value to global variable in one function. When I am trying to get that value from another function, it always returns null. Here my code is
StockList.php
<?php
$_current;
class StockList
{
public function report(){
global $_current;
$_current = 10;
}
public function getValue(){
print_r($GLOBALS['_current']);
}
}
?>
Suggestion.php
<?php
include ("StockList.php");
$stk = new StockList();
$stk->getValue();
?>
Thanks in advance.
Man, its hard to understand what are you trying to do as you said you have called report() in your index.php
Anyways, when dealing with classes, to set variable values, standard procedure is as following:
class StockList
{
public $_current;
public function setValue($value){
$this->current = $value;
}
public function getValue(){
return $this->current;
}
}
And after whenever you wanna use the class:
<?php
include ("StockList.php");
$stk = new StockList();
$stk->setValue(10);
$_current = $stk->getValue();
var_dump($_current);
?>
This is basic idea of OOP, benefits of this approach are:
You can dynamically set value of $_current.
Your getValue() function is not dedicated for printing the value of the variable, thats why you can use that function only for getting the value and then do whatever you want with it.

php:how to use a var outside a function inside a class

i made a function inside my class like that
public function admin_ads_manage(){
$ads728=$_POST['ads728'];
$ads600=$_POST['ads600'];
$ads300=$_POST['ads300'];
$file728=stripslashes(file_get_contents($this->dir_name."/ads/ads728.txt"));
$file600=stripslashes(file_get_contents($this->dir_name."/ads/ads600.txt"));
$file300=stripslashes(file_get_contents($this->dir_name."/ads/ads300.txt"));
if($_POST['submit']){
$f728=fopen($this->dir_name."/ads/ads728.txt",'w');
$w728=fwrite($f728,$ads728);
$f600=fopen($this->dir_name."/ads/ads600.txt",'w');
$w600=fwrite($f600,$ads600);
$f300=fopen($this->dir_name."/ads/ads300.txt",'w');
$w300=fwrite($f300,$ads300);
echo "<meta http-equiv=\"refresh\" content=\"0\" " ;
}
i want to use the variable (only) inside this function outside it to print the output in the index page,how to access to this function to allow to use only some var,not executing the whole function
i know i can execute the whole function by $object->function();
but i want only to use some var..........
private $myVar;
public function admin_ads_manage(){
$this->myVar = 'Your value';
// Rest of the code
}
public function getMyVar() {
return $this->myVar;
}
// Where you what to use it
echo $object->getMyVar();
Create public instance variable and access it.
class A
{
public $var;
public function admin_ads_manage()
{
//skip some code
$this->var = "value to be accessed from outside";
}
}

Categories