PHP object with $a->name and $a->name->function() [closed] - php

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

Related

PHP cent use global variable in class method [closed]

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

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

How to use keyword "static" to make this work as intended? [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
I am preparing for the ZEND Certified Engineer-Exam. Using the TestPassport-Engine "Virtual Exam", I came across this question:
Consider the following code. Which keyword should be used in line marked in bold to make this code work as intended?
abstract class Base {
protected function __construct() {}
public function create(){
// this line
return new self();
}
abstract function action();
}
class Item extends Base {
public function action () { echo __CLASS__; }
}
$item = Item::create();
$item->action();
And the correct answer is static. So, how should that look like in the end?
Simply change
public function create() {
return new self();
}
to
public static function create() {
return new static();
}
See here.

Need Help Converting .NET class structure to 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
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;
}
}
?>

Categories