Php OOP : Parent class values are always null - php

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

Related

How to use a variable from a function in a class

I have a function declared in another file. I need to get a variable from it and use it in the class. But for some reason an error appears.
$myvar = myfunc($text);//an error here
class func{
public $title = $myvar;//and here
public function ($title){
...
}
}
You can inject the return value of the function in the constructor when the object is being initialized.
include_once 'func.php';
class Myclass{
public $title;
function __construct( $title = null )
{
$this->title = $title;
}
}
$obj = new Myclass( getVar() );
echo $obj->title;
func.php class
<?php
function getVar()
{
return 'this_var';
}
?>
Your question is about how to use a variable inside a class
class MyClass{
public $title; //do not put a variable here, this is invalid
///you could set a default like this
// e.g. public $title = "Jam";
//this is a setter
public function setTitle(string $title){
$this->title = $title
}
public function getTitle() {
return $this->title;
}
}
then use
$class = new MyClass();
$class->setTitle("jam sponge");
echo $class->getTitle();
or
define a constructor
class MyClass{
public $title;
//this is a constructor
public function __construct(string $title){
$this->title = $title
}
public function getTitle() {
return $this->title;
}
}
then use
$class = new MyClass("jam sponge");
echo $class->getTitle();
the latter is my preference.
As for this $myvar = myfunc($text);//an error here that's not relate to passing the var to the class. You have some other issue in that function, so either show that code or post anew question specific to it

Echo variable which has been set in another classes function

I am trying to access the contents of a variable from another class. I have the code below, I am expecting to get 'test' returned, I get nothing.
I assume this is because it is getting $abc_rank as empty. It is required that the variable is populated in the function itself.
Therefore how can I get $abc_rank to hold that echo and output via the other class?
<?php
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
$this->abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
$go = new class2();
?>
I know I can do:
public static $abc_rank = 'test';
But the population of the variable must be in a function.
I have read some of the other related answers and can't seem to get this to work.
In class1 :
Replace $this->abc_rank = 'test'; with $this::$abc_rank='test';
($abc_rank is a static property)
In class2 :
In your display function : replace
$test = class1::$abc_rank;
echo $test;
with
$test = new class1();
echo $test::$abc_rank;
(class1 isn't static)
Full code here :
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
//$this->abc_rank = 'test';
$this::$abc_rank='test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
//$test = class1::$abc_rank;
//echo $test;
$test = new class1();
echo $test::$abc_rank;
}
}
$go = new class2();
you have to create the class1 to run the constructor of this class.
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
self::$abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
new class1();
$go = new class2();

Switch visibility in php if parameter

I'm wondering if its possible to switch the visibility in PHP. Let me demonstrate:
class One {
function __construct($id){
if(is_numeric($id)){
//Test function becomes public instead of private.
}
}
private function test(){
//This is a private function but if $id is numeric this is a public function
}
}
Is such thing even possible?
I would use an abstract class with two implementing classes: One for numeric and one for non-numeric:
abstract class One {
static function generate($id) {
return is_numeric($id) ? new OneNumeric($id) : new OneNonNumeric($id);
}
private function __construct($id) {
$this->id = $id;
}
}
class OneNumeric extends One {
private function test() {
}
}
class OneNonNumeric extends One {
public function test() {
}
}
$numeric = One::generate(5);
$non_numeric = One::generate('not a number');
$non_numeric->test(); //works
$numeric->test(); //fatal error
It can be faked up to a point with magic methods:
<?php
class One {
private $test_is_public = false;
function __construct($id){
if(is_numeric($id)){
$this->test_is_public = true;
}
}
private function test(){
echo "test() was called\n";
}
public function __call($name, $arguments){
if( $name=='test' && $this->test_is_public ){
return $this->test();
}else{
throw new LogicException("Method $name() does not exist or is not public\n");
}
}
}
echo "Test should be public:\n";
$numeric = new One('123e20');
$numeric->test();
echo "Test should be private:\n";
$non_numeric = new One('foo');
$non_numeric->test();
I haven't thought about the side effects. Probably, it's only useful as mere proof of concept.

parent class method returns null in its daughter __construct()

I tried a code which I called a parent method in its daughter __construct and itreturns NULL,
I dont know why? I would be very happy if anyone could explain to me why.
Thanks in advance.
Here is my code
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construc()
{
$this->bio = $this->setLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();
?>
When I call:
$mB = new myBio();
$mB->newLeg();
, it returns
NULL,
BUT
$mB->tryLeg();
returns e string, 'pretty'.
You have a typo on this line:
$this->bio = $this->setLeg();
You're calling your setter, not your getter, and since the setter doesn't return a value you're getting null instead.
You've also misspelled construct:
public function __construc()
And you need to call the parent constructor.
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construct()
{
parent::__construct();
$this->bio = $this->getLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();

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;

Categories