php how to use variable inside another class - php

Iam learning php these days. I have one query below:
class A {
var $a;
}
class B extends A{
var $b = $a; //here its showing me error, i even tried as '$this->$a' but still showing error. so, how do I use $a in class B? ( instead of using in a function ),
}
I am declaring $b just inside class instead of inside a function because I need to use $b variable in many places inside my php file.
So, please tell me how to fix this.

try like below
<?php
class firstname
{
public static $name='Your First Name';
}
class lastname
{
public static $last='Your Last Name';
}
class Fullname
{
public static function staticValue() {
return firstname::$name."--".lastname::$last;
}
}
print Fullname::staticValue() . "\n";
?>
hope it will help you

1.) use public instead of var
2.) when you extending a class, you have got access to properties of this parent. So you don't need to declare $b, you can just use $this->a
class a {
$a = 'test';
}
class b extends a {
public function get() {
echo $this->a;
}
}
$b = new b;
$b->get(); // prints test

Related

calling a class function from another class function

I'm new to programming. I have this going on:
I have Class A, which have many functions. One of those functions is functionX.
In functionX I need to make a call to functionY which belongs to another class: Class B.
So how do I acces to functionY from inside functionX?
I use Codeigniter.
Thanks in advance.
Try and experiment with this.
class ClassA {
public function functionX() {
$classB = new ClassB();
echo $classB->functionY();
}
}
class ClassB {
public function functionY() {
return "Stahp, no more OO, stahp!";
}
}
Class function? A static method?
If you have an instance (public) method, you just call $classB->functionY().
If you have a static method, you would call ClassB::functionY();
So:
class ClassA {
public function functionX(){
$classB = new ClassB();
// echo 'foo';
echo $classB->functionY();
// echo 'bar';
echo ClassB::functionYStatic();
}
}
class ClassB {
public $someVar;
public static $someVar2 = 'bar';
function __construct(){
$this->someVar = 'foo';
}
public function functionY(){
return $this->someVar;
}
public static function functionYStatic(){
return self::$someVar2;
}
}
Well that depends. If that function is a static function or not.
First off you must include the file with the class...
include_once('file_with_myclass.php');
If it is static you can call it like this:
ClassName::myFunction()
If it is not, then you create an instance of the class and then call the function on that instance.
$obj = new ClassName();
$obj->myFunction();
As you can guess the function being static means you can call it without the need of creating an instance. That is useful for example if you have a class Math and want to define a function that takes to arguments to calculate the sum of them. It wouldn't really be useful to create an instance of Math to do that, so you can declare as static and use it that way.
Here's a link to the docs with further info
http://www.php.net/manual/en/keyword.class.php
If functionY is static you can call ClassB::functionY(). Else you must create instance of Class B first. Like:
$instance = ClassB;
$instance->functionY();
But maybe you mean something else?
Looks like one of your class has a dependency to another one:
<?php
class A
{
public function x()
{
echo 'hello world';
}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function y()
{
$this->a->x();
}
}
$a = new A();
$b = new B($a);
$b->y();
Depending how your code looks like, if it makes sense, you can inject class A into y()
public function y(A $a)
{
// your code with $a
}

Display a PHP parent Class Variable

I would like to display a parent Class variable, I can't find a way to solve the situtation...
Here is my PHP :
class A {
public $a;
}
class B extends A {
public function __construct() {
echo $parent->a;
}
}
$B = new B();
This is supposed to output $a, in my case $a is an PDO object, and instead of print it, i call a prepare() on it :)
like that :
class A {
public $a;
}
class B extends A {
public function __construct() {
$this->a->prepare('random SQL request');
}
}
$B = new B();
I have a "Cannot access empty property" PHP error
Thanks !
echo $this->a;
Many times the comments in the PHP manual are as valuable as the manual itself:
http://www.php.net/manual/en/keyword.parent.php#42153
Something like this would work:
class A {
public $a = "Hello World";
}
class B extends A {
public function __construct() {
echo $this->a;
}
}
$B = new B();
Run it:
php parent.php
Hello World

How access outside variables from within class?

I have two class called them class A and B. I created the 'A' class. And in this i create a 'B' class. How can i access the 'A' class variable from 'B' class?
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
}
}
class B extends Writers
{
function __construct()
{
parent::__construct();
echo $letter; //This is where i want to acces outside variable (CLASS 'A')
}
}
I hope i was clear. I'm just rookie on OOP-ing. Please help me.
You can't, because $letter in A is was not declared public and B doesn't extend A.
If you don't want to expose A's data (one of the important OOP principles), you should use encapsulation. Create a getter in the A class
public function getLetter()
{
return $this->letter;
}
And then in B's construct method, create an instance of A and use said getter
$a = new A();
$letter = $a->getLetter();
In case you really need to use this structure, I guess you could do something like this:
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B($this);
}
}
class B extends Writers
{
var $a;
function __construct(A $a)
{
parent::__construct();
$this->a = $a;
echo $this->a->letter;
}
}
This way, B would hold a reference to the A object it was created by. However, I rather recommend you to change your class topology, if possible.
Your B class needs to somehow have a reference to an A object. You could simply add to B :
class B extends Writers
{
private $a;
function __construct()
{
parent::__construct();
echo $letter; //This is where i want to acces outside variable (CLASS 'A')
}
public function A($a)
{
$this->a = $a;
}
}
Next, simply have your A object give a reference to itself to its B object.
class A
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
$this->writers->A($this);
}
}
The alternative would be static method.
It's hard to tell exactly what you want, but as I interpreted it:
class A extends Writers
{
var letter;
var writers;
function __construct()
{
$this-letter = 'SOMETHING';
$this->writers = new B;
}
}
class B extends A
{
function __construct()
{
parent::__construct();
echo $this->letter; //This is where i want to acces outside variable (CLASS 'A')
}
}
You can use static variable. like this:
class A
{
static $letter;
function __construct()
{
self::$letter = 'SOMETHING';
}
}
$objA = new A();
class B
{
function __construct()
{
$letter = A::$letter;
echo $letter; //print SOMETHING
}
}

Change value of a variable of the parent class in the child class

class A {
protected $a;
// SOME CODE
}
class B extends A {
// SOME CODE
}
How can i edit the protected value of the variable $a inside the B class ?
I'm trying to use parent::$a = "Some Value" but doesn't work.
protected instance properties, those which where not declared using static, can be accessed in subclasses using $this :
class A {
protected $a;
// SOME CODE
}
class B extends A {
// SOME CODE
public function edit($val) {
$this->$a = $val;
echo "a is now {$this->a}\n";
}
}
call:
$b = new B();
$b->edit('foo'); // a is now foo
Refer to the manual, especially the examples.
class B extends A {
public function foo($val)
{
$this->a = $val;
}
}
quite simple :)
Remember that
Members declared protected can be accessed only within the class
itself and by inherited and parent classes
from php manual

How can I access my classe's var inside the class?

I have a class:
class MyClass {
public $a = 'blablabla';
}
And I want to access the variable $a inside the class without needing to use any function, like
class MyClass {
public $a = 'blablabla';
public $b = $a;
}
I tried using public $b = $this->a, public $b = MyClass->a, and many other alternative ways to try to do what I want, and nothing. And I didn't find anything on Google that could explain what I want.
Could someone please help me? Thanks.
I am not sure why a simple variable call will not work, but you can try:
class MyClass {
public $a = 'blablabla';
function geta(){
return $this->a;
}
$b=geta();
}

Categories