php output function return through classes - php

this is my code, semplified
I'm trying to output something, returned form other functions, but it doesn't work.
I've tried also using self:: instead of $this->
class a{
function a(){
return "aaaaaaaaaaaaaaaaah";
}
function b(){
return "bbbbbbb";
}
}
$a = new a;
class b{
function ea($a){
return "oajs$a";
}
function f(){
global $a;
$blah = $this->ea("asd");
$blah .= $a->a;
return $blah;
}
}
$b = new b;
echo $b->f;

This is what you are looking for.
class a {
public function a(){
return 1;
}
public function b(){
return 2;
}
}
class b{
public $a;
public function __construct() {
$this->a = new a();
}
public function ea($a){
return "ao$a";
}
public function f(){
$blah = $this->ea("stuff");
$blah .= $this->a->b();
return $blah;
}
}
$b = new b();
echo $b->f();

Related

Accessing constant of an injected class in PHP

class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
return $this->a::MY_CONSTANT;
}
}
Why the constant is not accessible like this way - $this->a::MY_CONSTANT? Anybody knows any other ways?
The above can be achieved in this way. Here we are using get_class function to get classname as string. which we are storing it in a variable and then retrieve the value of constant by using that variable.
Try this code snippet here
<?php
ini_set('display_errors', 1);
class A{
const MY_CONSTANT = 'my constant';
}
class B{
protected $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function someFunction()
{
$class=get_class($this->a);
echo $class::MY_CONSTANT;
}
}
$object=new B(new A());
$object->someFunction();
You can also do the same by this approach.
class A{
const MY_CONSTANT = 'my constant';
public function __get($key){
$r = new ReflectionObject($this);
if($r->hasConstant($key)){ return $r->getConstant($key); }
}
}
class B{
public function someFunction()
{
return new A();
}
}
$b = new B();
var_dump($b->someFunction()->MY_CONSTANT);
Original Answer Link

Get property from another class function?

<?php
//file classA.php
class A {
private $B;
public $data;
public function __construct(){
$this->B = new B();
}
public function readA(){
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
require_once .... classA.php
requier_once .... classB.php
$classA = new A();
$classA->readA();
I wanna use classes with multiple dependences.
Can't use instance methods or extends classes.
How can i get function result and send it back to the same class from another?
Your problem is an endless loop of object creation:
When you create class A it'll create an Object of Class B, that will create another object of Class A again, that will create an Object of Class B,....
-> Memory Error
So if you get rid of $this->B = new B(); in __construct() of class A it'll work with that change:
You had:
// in class B
public function readB(){
return $this->sendB();
}
// it needs to be:
public function readB(){
return $this->A->sendB();
}
Complete working code:
EDIT: Now with readA() in class A, but creation of class B out of constructor.
<?php
class A {
private $B;
public $data;
public function __construct(){
// $this->B = new B();
}
public function readA(){
$this->B = new B();
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
$B = new B();
echo $B->readB();
$A = new A();
echo $A->readA();
?>

PHP Class with properties that has objects be accessible to these objects

I have a main php class such as this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass();
$this->b = 'some string';
}
}
is there a way the class which is stored in the $a (SomeClass) property can access the $b value which is actually a property which is stored in the class that initiated $a (MyClass) ?
You could do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass($this);
$this->b = 'some string';
}
}
class SomeClass {
public $mc;
function __construct(MyClass $mc)
{
$this->mc = $mc;
}
}
$myClass = new MyClass();
echo $myClass->a->mc->b;
The output would be: some string
You can do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->b = 'some string';
$this->a = new \SomeClass($this->b);
}
}
class SomeClass{
function __construct($b)
{
echo $b; // it will become a $this->b as given while creating new class in MyClass
}
}

PHP Fatal error: Using $this when not in object context in C:/xampp/htdocs/oops/1.php on line 9?

I need to assign b value in a inside the method onec, but its failing. Please let me know what I am doing wrong here:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>
Use the self keyword. The $this keyword is not accessible under static context. Also, you should make your variables static
Like this..
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();
You use $this in static function.
http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();
Use this code
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();

PHP call function within private function in class method

I try call Test3 function, but returned this error: "Fatal error: Call to undefined function".
Here is an example:
class Test {
public Test1(){
return $this->Test2();
}
private Test2(){
$a = 0;
return Test3($a);
function Test3($b){
$b++;
return $b;
}
}
}
How to call Test3 function ?
From PHP DOC
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.
Use Closures 
$test = new Test();
echo $test->Test1();
Modified Class
class Test {
public function Test1() {
return $this->Test2();
}
private function Test2() {
$a = 0;
$Test3 = function ($b) {
$b ++;
return $b;
};
return $Test3($a);
}
}
Not sure if you wanted a closure or if your 'inner' function was a typo.
If it was meant to be a separate method then the below is the correct syntax:
class Test
{
public function Test1()
{
return $this->Test2();
}
private function Test2()
{
$a = 0;
return $this->Test3($a)
}
public function Test3($b)
{
$b++
return $b;
}
}

Categories