Jump to content

Recommended Posts

How to the hell do I use a variable that has been previously decalred within a function (method) within a class?

 

 

<?php
$var = "sometext";

class getvar{
  
function var() {
    echo "your var: $var";
}

}
?>

 

NOTE: class may be faulty- I wrote it on the spot. I have one working.

 

Love to know how to tackle this. It seems this damn variable scope just gets in the way.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/
Share on other sites

This seems to work for me providing you change the name of your function and make the $var global inside the function

 

var is a reserved word, i dont think the use of it is recommended now in php 5 but it was in php4

 


<?php
error_reporting(E_ALL);

$var = "sometext";

class getvar{
  
  
function test() {
   global $var;
    echo "your var: $var";
}

}

$obj = new getvar();
$obj->test();

?>

The entire point of objects is to encapsulate data, dragging data in from outside like that is a bad idea.

 

You'll want to pass the variable to your method.

 

<?php

class getvar {
  
  public function test($var) {
    echo "your var: $var";
}

}

$var = "sometext";
$obj = new getvar();
$obj->test($var);

?>

Alternately:

 

<?php

$var = "sometext";

class getvar {

protected $var;

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

public function test() {
	echo "Var: {$this->var}";
}

}

$obj = new getvar( $var );
$obj->test();

?>

The entire point of objects is to encapsulate data, dragging data in from outside like that is a bad idea.

 

You'll want to pass the variable to your method.

 

<?php

class getvar {
  
  public function test($var) {
    echo "your var: $var";
}

}

$var = "sometext";
$obj = new getvar();
$obj->test($var);

?>

 

in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site completley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db, basicaly i people freez the site into a project id and add that project id into new records which are inturn altered to compensate new relations big ass script 600 users. looking back on it i should have done what u said passed the data to the classes, but oh well lol. i would have passed the data to teh classes im trained in 2004-2007 so i know but the bespoke systems already have messed up ways

in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site copletley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db.

 

You can do this just as easily without mucking around in the global namespace. There's really no reason to use it in an object-oriented system ( beyond the top-level pages, obviously )

in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site copletley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db.

 

You can do this just as easily without mucking around in the global namespace. There's really no reason to use it in an object-oriented system ( beyond the top-level pages, obviously )

 

my manager got carried away with config files and thats where this came from, the aim is to have the config file read from a databse instead of a php file, so basicaly all teh defined data will be read into arrays and vars from a db empowering teh application to be sold in a box.

i have had alot of fun with java and i do understand but things often lead there own way unless you refactor ur code constantly. i work on about 30 sites with 600-1000 users and over 10 000 records each and am developing new sites every day with the same framework so yes our components evolve function > class the platform it was developed on is php 4 with myswl 4 but its hosted and now developed on a mixture of both so i need to compensate etc, but overall i agree full object orientation is teh way forward for reusability which is what we all need.

Everything starting with post #4 in this thread is off topic and has nothing to do with the problem that original poster has and is not helping him fix it.

 

im sorry i disagree, were talkign about how people can get to a point where they have used global variables in there classes and detering the next generation that will read these posts in years to come and leaving them a trail of history, if u wana be a history wiper like some other people in history u go ahead.

The most suggested line of PHP code that helps pinpoint why code is not working - error_reporting(E_ALL);

 

<< true

 

Debugging step #1: To get past the garbage-out=garbage-in stage in your code, you must check that the inputs to your code are what you expect.

 

<< true

 

The answers you see in a forum are not secrets. They were learned by reading documentation.

 

<< False

 

some things are creative

 

2/3 ok for a junior lol joke

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.