So say I have the following code,
$obj = new foo();
echo $obj;
class foo {
public function __construct()
{
return 'a';
}
}
How do I make $obj echo the string 'a'?
How do I make $obj refer to or equal what is returned by the object/class?
Need to return a value from a __construct(), and also a normal private function within another class. For example:
$obj2 = new foo2();
echo $obj2;
class foo2 {
public function __construct()
{
bar();
}
private bar()
{
return 'a';
}
}
Thanks!
you can use the magic __toString() method to convert your class to a representing string.
You should not return something in your constructor, __toString() is automaticly called if you try to use your instance as string (in case of echo).
from php.net:
<?php
// Declare a simple class
class TestClass
{
public $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function __toString()
{
return $this->foo;
}
}
$class = new TestClass('Hello');
echo $class;
?>
http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
Constructors in PHP are more like initialisation functions; their return value is not used, unlike JavaScript for instance.
If you want to change the way objects are normally echoed you need to provide the magic __toString() method:
class foo
{
private $value;
public function __construct()
{
$this->value = 'a';
}
public function __toString()
{
return $this->value;
}
}
A private method that would return the value can be used in a similar manner:
class foo2
{
private function bar()
{
return 'a';
}
public function __toString()
{
return $this->bar();
}
}
Related
I want $foo->display(); to display Hello World
Here is what I tried:
class MyAttribute
{
public function init($var)
{
$this->setString($var);
}
public function display()
{
$this->setString = $var;
}
}
$foo = new MyAttribute("Hello World");
$foo->display();
You need to use the return keyword to pass the variable back, however that is not your issue, consider the following:
class MyAttribute {
private $attr;
public function __construct($attr)
{
$this->attr = $attr;
}
public function get_attr()
{
return $this->attr;
}
}
$attr = new MyAttribute('Hello World');
echo $attr->get_attr();
The constructor executes first when the class is instantiated and we set the property $attr with the variable that is passed to said constructor.
In the get_attr functionm the important part to notice is the return keyword which I have linked you to the documentation for it above.
You don't necessarily need a constructor, you can add another function called set_attr which sets/changes the value of $attr but seeing as you are using the constructor in your original code, I've left it in.
Live Example
Repl
Reading Material
PHP OOP
You Can use __construct because PHP will automatically call the __construct() method/function when you create an object from your class.
we can provide a value for the $par property when we create our MyAttribute objects.
class MyAttribute
{
private $var;
public function __construct($var)
{
$this->var = $var;
}
public function display()
{
return $this->var;
}
}
$foo = new MyAttribute("Hello World");
echo $foo->display();
Say object of class B is attribute of class A. How can I call method of object of class A from method of object of class B? What would be nice solution without passing object link?
Thanks!
Here goes code sample:
class A{
var $b;
function __construct(){
$this->b = new B();
}
function f1(){
$this->b->f3();
}
function f2(){
echo 'hello!';
}
}
class B{
function f3(){
// call f2() method in object $obj(not new A())
}
}
$obj = new A();
$obj->f1();
You can use a static function
public static function f2{
echo 'hello!';
}
with f3 defined as
function f3(){
A::f2();
}
This may not ultimately be the solution you want, however. See more info here.
The only way you can access that instance's function is if you inject it on the B object as a dependency. You can inject it within the constructor, like this:
<?php
class A {
protected $b;
public function __construct() {
$this->b = new B($this);
}
public function f1() {
$this->b->f3();
}
public function f2() {
echo 'hello!';
}
}
class B {
protected $a;
public function __construct($a) {
$this->a = $a;
}
public function f3() {
$this->a->f2();
}
}
$obj = new A();
$obj->f1();
Can I do this?
class A {
public function foo() { echo "whatever";}
}
class B {
static public $var;
static function initVar($var) { self::$var = $var; }
static public function bar() { return self::$var->foo(); }
}
class C {
public function baz() {
$a = new A();
B::initVar($a);
echo B::bar(); // should print "whatever"
}
}
if not, is there any way to a static method to access an given object instance ?
Yes, you can. This is usually how Singleton (a pattern where a object only should be instantiated once) is implemented. Though this pattern is considered bad...
http://en.wikipedia.org/wiki/Singleton_pattern
Example (though within the same class...):
class A
{
private static $inst;
public static function instance()
{
if (self::$inst === NULL)
{
self::$inst = new A();
}
return self::$inst;
}
/* constructor etc */
}
$a = A::instance();
$a->someMethod();
?>
I would like to ask, if there is a way, to use variables in a class, that were declared out of it.
Example:
$foo = 'bar';
class foobar{
function example(){
echo "foo{$foo}";
}
}
$foobar = new foobar;
$foobar->example();
This code produces a notice: Notice: Undefined variable: foo
Is there a way to make it work? Or is there some work-around?
You could give this argument to your class with a constructor
class foobar{
private $foo;
public function __construct($name) {
$this->foo = $name;
}
}
and then use it.
Or what PeeHaa means, you could change your method to
function example($param){
echo "foo{$param}";
}
and call it like this
$foobar->example($foo);
Use a construct to import it or use the global keyword. You could do something like this:
$var = 'value';
class foobar {
private $classVar;
function __construct($param) {
$this->classVar = $param;
}
}
And initiate it like this:
$var = 'value';
$inst = new foobar($var);
Or you can use global variables (which I wouldn't recommend in this case) and do something like this:
$var = 'value';
class foobar {
global $var;
function show() {
echo $var;
}
}
UPDATE: To use a class within another class, it may be instantiated in the constructor if its instance is needed throughout implementation, or it may be instantiated only when needed.
To create a reference to another class inside the constructor, do something like this:
class class1 {
private $someVar;
function __construct() {
$this->someVar = 'success';
}
function doStuff() {
return $this->someVar;
}
}
class class2 {
private $ref;
private $val;
function __construct() {
$this->ref = new class1();
$this->val = $this->ref->doStuff();
// $this->val now holds the value 'success'
}
}
$inst = new class2(); // upon calling this, the $val variable holds the value 'success'
Or you can call it only when needed, like so:
class class1 {
private $someVar;
function __construct() {
$this->someVar = 'success';
}
function doStuff() {
return $this->someVar;
}
}
class class2 {
private $ref;
private $val;
function __construct() {
// do something
}
function assign() {
$this->ref = new class1();
$this->val = $this->ref->doStuff();
// $this->val now holds the value 'success'
}
}
$inst = new class2(); // the $val variable holds no value yet
$inst->assign(); // now $val holds 'success';
Hope that helps you.
Yes add
class foobar{
function example(){
global $foo;
echo "foo{$foo}";
}
}
** putting it in another class is better though, or passing it to the method you're using is better too **
I'm still learning OOP so this might not even be possible (although I would be surprised if so), I need some help calling another classes method.
For example in ClassA I have this method:
function getName()
{
return $this->name;
}
now from ClassB (different file, but in the same directory), I want to call ClassA's getName(), how do I do that? I tried to just do an include() but that does not work.
Thanks!
//file1.php
<?php
class ClassA
{
private $name = 'John';
function getName()
{
return $this->name;
}
}
?>
//file2.php
<?php
include ("file1.php");
class ClassB
{
function __construct()
{
}
function callA()
{
$classA = new ClassA();
$name = $classA->getName();
echo $name; //Prints John
}
}
$classb = new ClassB();
$classb->callA();
?>
If they are separate classes you can do something like the following:
class A
{
private $name;
public function __construct()
{
$this->name = 'Some Name';
}
public function getName()
{
return $this->name;
}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
function getNameOfA()
{
return $this->a->getName();
}
}
$a = new A();
$b = new B($a);
$b->getNameOfA();
What I have done in this example is first create a new instance of the A class. And after that I have created a new instance of the B class to which I pass the instance of A into the constructor. Now B can access all the public members of the A class using $this->a.
Also note that I don't instantiate the A class inside the B class because that would mean I tighly couple the two classes. This makes it hard to:
unit test your B class
swap out the A class for another class
You would need to have an instance of ClassA within ClassB or have ClassB inherit ClassA
class ClassA {
public function getName() {
echo $this->name;
}
}
class ClassB extends ClassA {
public function getName() {
parent::getName();
}
}
Without inheritance or an instance method, you'd need ClassA to have a static method
class ClassA {
public static function getName() {
echo "Rawkode";
}
}
--- other file ---
echo ClassA::getName();
If you're just looking to call the method from an instance of the class:
class ClassA {
public function getName() {
echo "Rawkode";
}
}
--- other file ---
$a = new ClassA();
echo $a->getName();
Regardless of the solution you choose, require 'ClassA.php is needed.
File 1
class ClassA {
public $name = 'A';
public function getName(){
return $this->name;
}
}
File 2
include("file1.php");
class ClassB {
public $name = 'B';
public function getName(){
return $this->name;
}
public function callA(){
$a = new ClassA();
return $a->getName();
}
public static function callAStatic(){
$a = new ClassA();
return $a->getName();
}
}
$b = new ClassB();
echo $b->callA();
echo $b->getName();
echo ClassB::callAStatic();