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
Related
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;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
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.
Improve this question
i am trying to make $var1 to work on many different method
var1 is outcome of extract() from an array that contains url parts
exp:
$url = 'localhost/site/className/edit/254/...';
$arr[var1] = 'edit';
$arr[var2] = '254';
$var1 = 'something';
class myClass{
function doSomething(){
echo $var1;
}
}
$obj = new myClass();
$obj->doSomething();
output:
Notice: Undefined variable: var1 in....
is there any way to fix it??
2 ways to fix it:
First, the best - passing variables as function arguments:
$var1 = 'something';
class myClass{
function doSomething($var){
echo $var;
}
}
$obj = new myClass(); //You could also pass it to constructor
$obj->doSomething($var1);
Second, working, but is considered to be a bad practice:
$var1 = 'something';
class myClass{
function doSomething(){
global $var1 ;
echo $var1;
}
}
$obj = new myClass();
$obj->doSomething();
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
why this code does not print the value.
class Test{
var $i;
function Test($i){
$this->i=$i;
}
function func1(){
echo $i;
}
}
$ob1=new Test(4);
$ob1->func1();
?>
Here I am using object oriented concept
You should echo $this->i not $i
function func1() {
echo $this->i;
}
Output
4
Replace
function func1(){
echo $i;
}
with
function func1(){
echo $this->i;
}
and will work fine try to learn using
http://php.net/manual/en/language.oop5.php
http://www.tutorialspoint.com/php/php_object_oriented.htm
See The Basics from the Classes and Objects section of the PHP language reference.
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
So use
function func1(){
echo $this->i;
}
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
<?php
class foo
{
public $a;
public function __construct()
{
$a='value';
}
}
$class = new foo();
echo $class->$a;
?>
I want to use the value $a in other parts of my script.
How do I retrieve a variable set in a php object and use it for other things outside of the object?
To set the value inside a method, the syntax is:
$this->a = 'value';
To obtain the value of the property from an instance of the class, the syntax is:
$foo = new foo();
echo $foo->a;
The Properties manual page goes into more detail.
I'd recommend using a getter (see):
Inside your class.
private $a;
public function getA()
{
return $this->a;
}
Outside your class.
$class = new foo();
echo $class->getA();
Important: Inside your class you should refer to $a as $this->a.
Use $this variable to assign and reference instance variables and functions on the current object.
In your case replace $a = 'value'; with $this->a = 'value';
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
I was hoping someone can help me achieve the same structure in php. Here is what I do in my .NET class.
Public Class objSample
Private _MyPrivateVar As Boolean = False
Public Property MyPublicProperty As Boolean
Get
Return _MyPrivateVar
End Get
Set(ByVal value As Boolean)
_MyPrivateVar = value
End Set
End Property
End Class
Here it is:
class objSample
{
private $_MyPrivateVar = false;
public $_MyPublicProperty;
public function getMyPrivateVar()
{
return $this->_MyPrivateVar;
}
public function setMyPrivateVar($val)
{
$this->_MyPrivateVar = $val;
}
}
I have no idea what's the purpose of this code, having lack of VB knowledge, it seems it has practically non use. However, I tried to make it as I think it has to work. Maybe someone more experienced with Visual Basic could help too. But, have in mind, we are not here to convert code. You should investigate PHP for your purpose.
<?php
class objSample {
private $_myPrivateVar = false;
public $myPublicProperty;
public function __get() {
return $this->_myPrivateVar;
}
public function __set($value) {
$this->_myPrivateVar = $value;
}
}
?>