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

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";
}
}

Related

How to access function value in another function in PHP Classes

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();

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;
}

Can I make a PHP function "private" in a script?

I want some functions to be not visible when required, but visible in the script they belong to.
For example:
required.php:
<?php
function privateFunction() {
echo 'from private function\n';
}
echo 'from required.php: ';
privateFunction();
index.php:
<?php
require './required.php';
echo 'from index.php: ';
privateFunction(); // I want this to give an error like "private function called outside the script it has been declared".
I have already tried making the function private, but it only gives a parse error.
required.php:
<?php
class A {
private function privateFunction() {
echo 'from private function\n';
}
}
This is a probably a bad idea in general (see comments above), but the following should do what you're asking.
It makes use of debug_backtrace() to grab the file name of the function then the file name of the function's caller, and compares them. If they don't match, it throws an exception:
function assertCalledByCurrentScript(): void
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
if (isset($backtrace[1]) && $backtrace[0]['file'] !== $backtrace[1]['file']) {
throw new \BadFunctionCallException("Cannot call {$backtrace[1]['function']} from outside of its defining script.");
}
}
Then use it like this:
function privateFunction()
{
assertCalledByCurrentScript();
// rest of your "private" function here
}
Basically only option you have is to hide private stuff inside class:
//Your file
class MyClass
{
public static function myAction()
{
$internalStuff = self::internalStuff();
return 'Using this function you can get what is inside internalStuff: ' . $internalStuff;
}
private static function internalStuff()
{
return 'Some internal stuff not accessible from outside';
}
}
function myAction()
{
return MyClass::myAction();
}
//Otherfile
//require yourfile.php
//You can only use `myAction` as function
echo myAction();
//Or call
echo MyClass::myAction();
//But you cannot use `internalStuff
echo MyClass::internalStuff();

PHP OOP print a variable which has value inside the function but call from outside to be print

Is it possible to print a variable which has the value inside the function but it's called from outside the function to be print in object oriented programming in PHP
Let's explain by example
My class looks like as:
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo $this->a;
}
}
It should print the value of $name when the function is call, as I am trying:
$class = new my();
$class->a = '$name';
$class->myFunc();
But it did't work and print the result as:
$name
I want it should print the value of variable $name which is inside the function
How it can be possible?
Thank You.
You can use variable variables to do this, but it's usually considered bad practice.
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo ${$this->a};
}
}
$class = new my();
$class->a = 'name';
$class->myFunc();
Output:
fahad
Inside your function, you can make a check:
public function myFunc(){
if($this->a == '$name'){
$name = 'fahad';
echo $name;
}else echo $this->a;
}

Categories