Not working Lang Changer in a Class Function PHP [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello i try to implent a Language Changer in my Script.
But in my Class Function it is saying:
Notice: Undefined variable: lang in ......
I use a if else at begin of my Index.php which decide to includes the correct lang_xxx.php.
And in my lang_xx.php there is Array
/*
-----------------
Language: English
-----------------
*/
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
And echo in my Class then
<?php echo $lang['welcome_msg']; ?>
So my Question why isnt this working and what is the correct way to implent this?
Its also not working when i include the lang_xx.php File directly in the Class Function.

Use 'global' to declare that the variable exists outside of the class.
$lang = array();
$lang['welcome_msg'] = 'Welcome Guest';
class Foo {
function bar(){
global $lang; // declare $lang as global, inside the method
echo $lang[]...
}
}

Related

php class not saving variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a simple API class that talks with its web-based counterpart.
All works fine, just one variable denies to be saved.
I have a variable called $scope, initialised at the top of the class:
class Api {
private $scope;
public function set_scope( $s ) {
$this->scope = serialize($s);
return true;
}
private function get_scope() {
return unserialize($this->scope);
}
}
Next I'm getting the scope via API from the web script as JSON, I json_decode it as an array (second parameter as true) and in that form, I pass it through set_scope() function.
I am 10000000% sure API returns JSON, when var_dump'ed it returns me a proper array data.
For some reason though, that data is not saved into the $scope variable.
Any ideas?
Edit #1:
I call function get_scope within the class, in another function. I was trying to set that variable directly, so just using $this->scope=$scope, without success though. I use the same structured functions to save/get other variables, and all of them are working, except this one.
Other function looks like that:
public function get_modules() {
$av_modules = $this->available_modules;
$scope = $this->get_scope();
var_dump($scope);
foreach($scope as $mod) {
$av_modules[$mod] = true;
}
return $av_modules;
}
The get_scope method is private (thus cannot be called from outside itself).
Make the get_scope method public and the value will be returned.
Your get_scope() function is declare as private. To use it outside class, you should change it to public.

PHP __autoload() function not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i want to use __autoload() but it is not working here is the code
function __autoload($class)
{
require_once($class.".php");
}
and here create object
$obj = new MyClass();
Try this code.
function __autoload($class)
{
require_once($class.".php");
}
spl_autoload_register('__autoload');
$obj = new MyClass();
Try this. it works for me.
if (!function_exists ("__autoload"))
{
function __autoload($classname)
{
require_once($class.".php");
}
}

Call function inside class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to create a simple class in PHP, but i've got some trouble with a method call.
<?php
include('MySQL.php');
class User {
var $sql;
function _construct(){
// SQL connection
$this->sql = new MySQL(<<hidden>>, <<hidden>>, <<hidden>>);
}
public function login($username, $password){
// TODO
}
}
?>
At the //TODO section, i want to do a call like $this->sql->select('users'), but it won't let me do it. It gives an error and says that sql is a non-object.
Your "_construct" is missing a _ (you must have two), to it won't get called. Change it to :
public function __construct(){
It should work. Also remember to make it public.
If it's not called, you $sql variable isn't initialized and is actually a non-object for PHP.
Also, you may precise the visibility of your variable when declaring it, rather than using the deprecated var keyword :
private $sql;

PHP: don't want to show __construct or any variable inside the _construction function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I created a class with a constructor (__construct()), but I don't want anyone to be able to access it. How can I do that? Thank you very much!
Edit 1:
For more detail: I created a class:
<?php
class test{
function __construct()
{
$a=1;
}
}
$t = new test;
$t->//here's the problem
?>
In my editor, when press $t->, the code hint shows the ('_construct()') and ('$a') too.
I want to ask: Can someone else can acces ('$a') or ('_construct()').
How can I prevent that,
Just make the constructor private
class Test {
private function __construct() {}
}
If you don't let any access the constructor function for your class - no one will be able to use that class as they will not be able to instantiate it.
In any case, if they have your class file they will be able to look at the source code and see the constructor.

php undefined variable for a function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
so as i mentioned in comments the first one shows but the second one does not show and gives me unidentified error i could just pass it to the function but is that normal ?
echo $add;//this one shows normally
if($_SERVER['REQUEST_METHOD']=="POST")
if($_POST['formname']=="Signup Form")
SignUp();
else if($_POST['formname']=="Signin Form")
SignIn();
function SignUp(){
echo $add; //this one give me unidentified erro
$bool = true;
//some code
}
function SignUp()
{
to
function SignUp()
global $add;
{
Or, better, change it to:
function SignUp($add)
{
and change your call to SignUp($add);.
The first version (with global) appears to be much tidier, but it makes things much more complex as time goes on. In hindsight I wish I hadn't learnt about global so early on – it's rarely (ever?) the right approach.
Try declaring it as a global in the function:
function SignUp(){
global $add;
echo $add;`
You can pass it as argument
function SignUp($add)
{
echo $add;
}
or Make it global
function SignUp()
{
global $add;
echo $add;
}
You can dig more into Variable Scope here
Read princip of variable scope visibilities in php, answer on your question is second example. In body of function you're looking for local scope variable, you can easy make your variable global.
http://www.php.net/manual/en/language.variables.scope.php

Categories