PHP call function within private function in class method - php

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

Related

In PHP does return false bubble up?

I have a class with some methods in php.
One public method calls a protected method. If the protected method returns false will the public method return false and not continue?
public static function a() {
$class = getClass();
// some more code...
}
protected static function getClass() {
$classList = self::find_by_sql("
SELECT *
FROM ".self::$table_name."
WHERE Class_Closed = FALSE
ORDER BY Start_Date ASC
;");
if (empty($classList)) {
return false;
} else {
return $classList[0];
}
}
No. return isn't like an exception, and there is no bubbling. If you don't EXPLICITLY have a return, then there's an implicit return null;:
php > function foo() { }
php > var_dump(foo());
NULL
php > function bar() { $x = 42; }
php > var_dump(bar());
NULL
php > function baz() { return 'hi mom'; }
php > var_dump(baz());
string(6) "hi mom"
This holds true no matter how/where you define the function, including as a class method:
php > class foo { function bar() { } }
php > $foo = new foo();
php > var_dump($foo->bar());
NULL
No. $class will have a false value but you still need to return it from YourClass::a() if you want the method to terminate and return that value immediately. return only is in scope of the function/method it is called from.
public static function a(){
$class = getClass();
if (!$class) {
return false; // or return $class;
}
some more code...
}

php output function return through classes

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

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

passing variables from a protected function to a public function inside the same class in php

I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;

transfer variables between class A and B

How i can run method $this->ob->getVar() inside class B function C here ? I get no. Did i must transfer string to constructor ?
<?php
class A{
public $tabb = array('1'=>'one', '2'=>'two');
public $index;
public function setVar($v){
$this->index = $v;
}
public function getVar(){
return $this->index;
}
public function arr(){
return $this->tabb;
}
}
class B{
public $tab;
public function __construct($var){
$this->ob=new A;
$this->tab = $var;
}
public function C(){
return $this->D($this->tab, $this->ob->getVar());
}
public function D($l, $j){
if(is_array($l) && isset($j)){
print 'yes';
} else {
print 'no';
}
}
}
$obb = new A;
$obb->setVar('onetwo');
$k = $obb->arr();
$obbb = new B($k);
$obbb->C();
?>
First, for the sake of convention your B class should declare a private variable of $obj, but that is not necessary in PHP.
Second, your B class is just creating a new instance of A in its constructor. So you have two different A classes. The once inside B never has its index property populated.
If you wanted to have the A object created outside the B object you'll have to pass it in like this:
$obbb = new B($k, $obb);
So now your new B constructor is something like this:
public function __construct($var, $someObject){
if (!empty($someObject)) {
$this->ob = $someObject;
}
else {
$this->ob=new A;
}
$this->tab = $var;
}

Categories