Union results of two functions in php class - php

I have php class(simple example):
<?php class test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1 {
//some code
return 1;
}
public function echo2 {
//some code
return 2;
}
}
How could I return results of this two functions echo1 and echo2 in class in one row don't creating two new objects for each function?

$obj= new Test;
echo $obj->echo1().$obj->echo2();
Also, capitalize class names, and you'll need parentheses on those functions:
class Test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1() {
//some code
return "Hello";
}
public function echo2() {
//some code
return "World";
}
}
$obj= new Test;
echo $obj->echo1()." ".$obj->echo2();

You may have to change your functions a little bit.
...
public function echo1() {
//some code
echo 1;
return $this;
}
public function echo2() {
//some code
echo 2;
return $this;
}
...
Then call it like this.
(new test())->echo1()->echo2();

Related

Php OOP : Parent class values are always null

I have two php classes in two separate files:
File Name : ld.php
<?php
class LandDetail_Model{
public function __construct() {}
private $id;
public $pId;
private $bigha;
private $katha;
function setId($id) { $this->id = $id; }
function getId() { return $this->id; }
function setPId($pId) { $this->pId = $pId; }
function getPId() { return $this->pId; }
function setBigha($bigha) { $this->bigha = $bigha; }
function getBigha() { return $this->bigha; }
function setKatha($katha) { $this->katha = $katha; }
?>
File Name :Land_Detail.php
<?php
require_once '../models/ld.php';
class LandDetail extends LandDetail_Model{
public function __construct() {
parent::__construct();
}
public function DoSomething(){
echo "This is a value from Parent".$this->getPId();
}
}
?>
Now in SomeFile.php I am doing something like this.
<?php
include 'Land_Detail.php';
$ld = new LandDetail();
$ld->setPId(10001);
$ld->DoSomething();
?>
Why $this->getPId() is always returning null value? What is wrong with my code here? What is the correct way to extend a class in php from different file?
Why you write:
$ld->setPId = 10001;
Instead of
$ld->setPId(10001);

PHP how can make an function like this? first()->callSecond();?

I try to make an function like this below how can do that, any example?
$myClass->first()->callSecond();
You just need to return object all the time. It's called fluent interface. It can be self or other object.
<php
class A
{
public function first()
{
// Do something
return $this;
}
public function callSecond()
{
// Do somewthing else
return $this;
}
}
$a = (new A())->first()->callSecond();
<?php
class OtherClass{
public function callSecond(){
echo 'Second Called';
}
}
class MyClass{
public function first(){
return new OtherClass();
}
}
$myClass = new MyClass();
$myClass->first()->callSecond();
?>

Trying to execute a function in the constructor

The point is to execute a function in the constructor, like this for example.
$clas = new andrestest();
class andrestest{
function __construct(){
FunctionName();
}
public function FunctionName()
{
echo 10;
}
}
You can do two things
function __construct(){
$this->FunctionName();
}
or
function __construct(){
self::FunctionName();
}

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;

What happens if you call a constructor from a destructor?

Calling __construct() function from __destruct(),
<?php
public function __construct() {
echo "Hi";
}
public function __destruct() {
$this->__construct();
}
?>
will it create infinite loop?
No, but this will:
class Test {
public function __construct() {
echo "Hi";
}
public function __destruct() {
new Test();
}
}
new Test();
Example: http://ideone.com/94XUg
No, it won't. __construct is just regular function while called directly instead of using new ClassName;

Categories