PHP inherit method which return $this; - php

I am searching for the correct way to the next code in PHP:
class X {
var $A = array();
function Y() {
$this->A[] = array('price'=>1);
return $this;
}
}
class Y extends X {
var $VAT = 1.27;
function Y() {
parent::Y(); //at this point what is the correct way to call parent method?
foreach ($this->A AS $akey => $aval) {
if (is_array($aval)&&(array_key_exists('price',$aval))) {
$this->A[$akey]['price'] = $aval['price'] * $this->VAT;
}
}
return $this;
}
}
When I call parent method with "parent::Y();" I think this will not the correct way in PHP because it will return with an object which not ordered to any variable identifier and may cause a warning or a notice in error log.
Have anybody a good advice for what is the correct way to call method in this situation - Without modifying class X?

If you need to call parent method you can simply do it without a problem using parent::method_name().
As in this case $A property is public (var used) it will be visible also in child class.
You don't need to worry that method Y returns $this. It's used probably for chaining class methods and you don't use it here so you don't need to care about it.

As far as the official documentation goes, parent::method(); is the way to go.

The method is fine as it is. No need to assign the result of parent::Y() to a variable.

Related

Assign value directly in php constructor with a external function

Can I assign to a property a value in the constructor, without define any parameter, using for instance an external function?
Example
function my_external_function() {
return 'Good Morning World';
}
class MyClass {
protected $_my_property;
public function __construct() {
$this->_my_property = my_external_function() != '' ? my_external_function() : 'Good night World!';
}
public function getOtherMethod() {
return $this->_my_property;
}
}
$obj = new MyClass();
echo $obj->getOtherMethod();
You can do this. The code in your question will work, but there is a problem with this approach.
If you write your class this way, it will always depend on that external function, but it will have no control over whether or not it even exists, let alone whether or not it will return a value the constructor can use. If you move, rename, or modify the external function, it could change the behavior of your class in unpredictable ways.
I would recommend something like this instead, which I think may accomplish what you're trying to accomplish (not sure) without forcing your class to blindly depend on an external function.
class MyClass {
protected $_my_property = 'Good night World!'; // set a default value here
public function __construct($x = null) { // give your constructor an optional argument
if ($x) { // use the optional argument if it's provided
$this->_my_property = $x;
}
}
public function getOtherMethod() {
return $this->_my_property;
}
}
You can still create an instance of your class with no argument
$obj = new MyClass();
and when you call $obj->getOtherMethod(); you'll get the default value.
You can still use the external function; just let it pass its value into your object's constructor instead of using it within the constructor.
$obj = new MyClass(my_external_function());
Yes, but you better avoid such tricky dependencies.

How to call a method after another method in one line

I was looking to some php codes, and I saw an object that will call multiple methods in the same line.
I've tried to understand how to do it, and why we need to use it?
$object->foo("Text")->anotherFoo()->bar("Aloha")
What this styling called? and what is the best way to use it in php applications.
This syntax is called method chaining, and it's possible because each method returns the object itself ($this). This is not necessarily always the case, it's also used to retrieve an object's property that in turn also can be an object (which can have properties that are objects, and so on).
It is used to reduce the amount of lines that you need to write code on. Compare these two snippets:
Without chaining
$object->foo("Text");
$object->anotherFoo();
$object->->bar("Aloha");
Using method chaining
$object->foo("Text")->anotherFoo()->bar("Aloha");
this is used when the first function returns an object that will contains the second function that will return another object and so on...
class X
{
public function A()
{
echo "A";
}
public function B()
{
echo "B";
}
}
class Y
{
public function A()
{
echo "Y";
}
public function B()
{
return $this;
}
}
$y = new Y();
$y->B()->A();//this will run
$x = new X();
$x->A()->B();//this won't run, it will output "A" but then A->B(); is not valid

PHP object method doesn't behave as I expect

I can't quite understand why the output of this code is '1'.
My guess is that php is not behaving like most other OO languages that I'm used to, in that the arrays that php uses must not be objects. Changing the array that is returned by the class does not change the array within the class. How would I get the class to return an array which I can edit (and has the same address as the one within the class)?
<?php
class Test
{
public $arr;
public function __construct()
{
$this->arr = array();
}
public function addToArr($i)
{
$this->arr[] = $i;
}
public function getArr()
{
return $this->arr;
}
}
$t = new Test();
$data = 5;
$t->addToArr($data);
$tobj_arr = $t->getArr();
unset($tobj_arr[0]);
$tobj_arr_fresh = $t->getArr();
echo count($tobj_arr_fresh);
?>
EDIT: I expected the output to be 0
You have to return the array by reference. That way, php returns a reference to the array, in stead of a copy.
<?php
class Test
{
public $arr;
public function __construct()
{
$this->arr = array();
}
public function addToArr($i)
{
$this->arr[] = $i;
}
public function & getArr() //Returning by reference here
{
return $this->arr;
}
}
$t = new Test();
$data = 5;
$t->addToArr($data);
$tobj_arr = &$t->getArr(); //Reference binding here
unset($tobj_arr[0]);
$tobj_arr_fresh = $t->getArr();
echo count($tobj_arr_fresh);
?>
This returns 0.
From the returning references subpage:
Unlike parameter passing, here you have to use & in both places - to
indicate that you want to return by reference, not a copy, and to
indicate that reference binding, rather than usual assignment, should
be done
Note that although this gets the job done, question is if it is a good practice. By changing class members outside of the class itself, it can become very difficult to track the application.
Because array are passed by "copy on write" by default, getArr() should return by reference:
public function &getArr()
{
return $this->arr;
}
[snip]
$tobj_arr = &$t->getArr();
For arrays that are object, use ArrayObject. Extending ArrayObject is probably better in your case.
When you unset($tobj_arr[0]); you are passing the return value of the function call, and not the actual property of the object.
When you call the function again, you get a fresh copy of the object's property which has yet to be modified since you added 5 to it.
Since the property itself is public, try changing:
unset($tobj_arr[0]);
To: unset($t->arr[0]);
And see if that gives you the result you are looking for.
You are getting "1" because you are asking PHP how many elements are in the array by using count. Remove count and use print_r($tobj_arr_fresh)

Assigning a function's result to a variable within a PHP class? OOP Weirdness

I know you can assign a function's return value to a variable and use it, like this:
function standardModel()
{
return "Higgs Boson";
}
$nextBigThing = standardModel();
echo $nextBigThing;
So someone please tell me why the following doesn't work? Or is it just not implemented yet? Am I missing something?
class standardModel
{
private function nextBigThing()
{
return "Higgs Boson";
}
public $nextBigThing = $this->nextBigThing();
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing; // get var, not the function directly
I know I could do this:
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing(); // Call to the function itself
But in my project's case, all of the information stored in the class are predefined public vars, except one of them, which needs to compute the value at runtime.
I want it consistent so I nor any other developer using this project has to remember that one value has to be function call rather then a var call.
But don't worry about my project, I'm mainly just wondering why the inconsistency within PHP's interpreter?
Obviously, the examples are made up to simplify things. Please don't question "why" I need to put said function in the class. I don't need a lesson on proper OOP and this is just a proof of concept. Thanks!
public $nextBigThing = $this->nextBigThing();
You can only initialize class members with constant values. I.e. you can't use functions or any sort of expression at this point. Furthermore, the class isn't even fully loaded at this point, so even if it was allowed you probably couldn't call its own functions on itself while it's still being constructed.
Do this:
class standardModel {
public $nextBigThing = null;
public function __construct() {
$this->nextBigThing = $this->nextBigThing();
}
private function nextBigThing() {
return "Higgs Boson";
}
}
You can't assign default values to properties like that unless that value is of a constant data type (such as string, int...etc). Anything that essentially processes code (such as a function, even $_SESSION values) can't be assigned as a default value to a property. What you can do though is assign the property whatever value you want inside of a constructor.
class test {
private $test_priv_prop;
public function __construct(){
$this->test_priv_prop = $this->test_method();
}
public function test_method(){
return "some value";
}
}
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel(); // corection
echo $standardModel->nextBigThing();

Function inside a function.?

This code produces the result as 56.
function x ($y) {
function y ($z) {
return ($z*2);
}
return($y+3);
}
$y = 4;
$y = x($y)*y($y);
echo $y;
Any idea what is going inside? I am confused.
X returns (value +3), while Y returns (value*2)
Given a value of 4, this means (4+3) * (4*2) = 7 * 8 = 56.
Although functions are not limited in scope (which means that you can safely 'nest' function definitions), this particular example is prone to errors:
1) You can't call y() before calling x(), because function y() won't actually be defined until x() has executed once.
2) Calling x() twice will cause PHP to redeclare function y(), leading to a fatal error:
Fatal error: Cannot redeclare y()
The solution to both would be to split the code, so that both functions are declared independent of each other:
function x ($y)
{
return($y+3);
}
function y ($z)
{
return ($z*2);
}
This is also a lot more readable.
(4+3)*(4*2) == 56
Note that PHP doesn't really support "nested functions", as in defined only in the scope of the parent function. All functions are defined globally. See the docs.
Not sure what the author of that code wanted to achieve. Definining a function inside another function does NOT mean that the inner function is only visible inside the outer function. After calling x() the first time, the y() function will be in global scope as well.
This is useful concept for recursion without static properties , reference etc:
function getRecursiveItems($id){
$allItems = array();
function getItems($parent_id){
return DB::findAll()->where('`parent_id` = $parent_id');
}
foreach(getItems($id) as $item){
$allItems = array_merge($allItems, getItems($item->id) );
}
return $allItems;
}
It is possible to define a function from inside another function.
the inner function does not exist until the outer function gets executed.
echo function_exists("y") ? 'y is defined\n' : 'y is not defined \n';
$x=x(2);
echo function_exists("y") ? 'y is defined\n' : 'y is not defined \n';
Output
y is not defined
y is defined
Simple thing you can not call function y before executed x
Your query is doing 7 * 8
x(4) = 4+3 = 7 and y(4) = 4*2 = 8
what happens is when function x is called it creates function y, it does not run it.
function inside a function or so called nested functions are very usable if you need to do some recursion processes such as looping true multiple layer of array or a file tree without multiple loops or sometimes i use it to avoid creating classes for small jobs which require dividing and isolating functionality among multiple functions. but before you go for nested functions you have to understand that
child function will not be available unless the main function is executed
Once main function got executed the child functions will be globally available to access
if you need to call the main function twice it will try to re define the child function and this will throw a fatal error
so is this mean you cant use nested functions? No, you can with the below workarounds
first method is to block the child function being re declaring into global function stack by using conditional block with function exists, this will prevent the function being declared multiple times into global function stack.
function myfunc($a,$b=5){
if(!function_exists("child")){
function child($x,$c){
return $c+$x;
}
}
try{
return child($a,$b);
}catch(Exception $e){
throw $e;
}
}
//once you have invoke the main function you will be able to call the child function
echo myfunc(10,20)+child(10,10);
and the second method will be limiting the function scope of child to local instead of global, to do that you have to define the function as a Anonymous function and assign it to a local variable, then the function will only be available in local scope and will re declared and invokes every time you call the main function.
function myfunc($a,$b=5){
$child = function ($x,$c){
return $c+$x;
};
try{
return $child($a,$b);
}catch(Exception $e){
throw $e;
}
}
echo myfunc(10,20);
remember the child will not be available outside the main function or global function stack
After using second time function with nested we get "redeclare" error. But.. We can use this like that:
function x( $some_value ){
$nested_function = function($x){ return $x*2; };
return $nested_function( $some_value );
}
Lukas is correct, they are globally defined even when wrapped in a method, thus doesn't make much sense:
<?php
class C {
function parent() {
function child($a,$b) {
return $a * $b;
}
return child(3,4);
}
}
$O = new C;
echo $O->parent();
echo ',';
//this unfortunately works..
echo child(3,4);
//now throw a redeclare error...
echo $O->parent();

Categories