PHP cent use global variable in class method [closed] - php

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

Related

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

Can you call a class from another class? [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
Im currently learning oop from youtube etc and ive started my first project using it. The question i have that hasn't been answered along the way is..
say i have two classes
class a{
public function dosomething($var){
}
}
and
class b{
}
Can i access dosomething function from class b? if so could someone point me in the right direction.
Thanks
You have two options:
Pass instance of class a to class b and call method on that. (advisable)
Make method in class a static and call it like a::method(). (you should never do this)
To solve problem with the first way, your b class needs slight modification:
class b{
public function callMethodOfClassa(a $instanceOfa, $var) {
$instanceOfa->dosomething($var);
}
}
Or:
class b {
private $property;
public function callMethodOfClassa($var) {
$this->property->dosomething($var);
}
public function __construct(a $instanceOfa) {
$this->property = $instanceOfa;
}
}
In the second example you keep reference to passed instance in a field called $property here and is passed when instance of b is initialized:
$instanceOfa = new a();
$instanceOfb = new b($instanceOfa);
For better understanding of object oriented programming with php read the manual
And promised demo for the first example demo for the second sample (for better understanding made name changes).
Detailed explenation from comment:
class a{
public function dosomething(b $var){
$var->dosomething2();
}
public static function dosomething3(b $var){
$var->dosomething2();
}
}
and
class b{
public function dosomething2(a $var){
echo 'Hi, not doing anithing with this var!';
}
}
usage:
$variable1 = new a();
$variable2 = new b();
$variable1->dosomething($variable2);
a::dosomething3($variable2); //static call, no instance
$variable2->dosomething2($variable2);

I want to find the correct way for using object oriented in php [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
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;
}

How to set a variable within a class, then access/refer to it? PHP [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
<?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';

PHP object with $a->name and $a->name->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
How can I create an object/class which has a variable. The variable then needs to also have a callable function.
$a->name //return foo
$a->name->getAlias() //return foobar
The above is an example and not the desired functionality.
Thank you.
The _toString automagically can change your object if your object is being called as an string. The name needs to be an instance of a object in order to use it different ways:
class A
{
function __construct()
{
$this->name = new B();
}
}
class B
{
function __toString()
{
return 'Jamie';
}
function getAlias()
{
return 'JJAMMIIEE';
}
}
$a = new A();
print $a->name; //returns Jamie
var_dump($a->name); //returns Object B, __toString function will not be called
print $a->name->getAlias(); //returns JJAMMIIEE
Documentation here

Categories