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();
}
Related
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
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
}
I'd like do do this:
class A {
public static $var = 'foo';
}
class B {
private $a;
public function __construct($a) {
$this->a = $a;
}
public function f() {
echo $this->a::$var; // <---- ERROR
echo get_class($this->a)::$var; // <---- ERROR
// WORKING but awful
$a = $this->a;
echo $a::$var;
}
}
$b = new B(new A());
$b->f();
Note that I don't know if $a is an instance of A or another class, I just know that it has a static $var member. So, unfortunately, I can't use A::$var inside f.
Does anyone know if there is a single-expression syntax to do this, using PHP 5.3+?
You're passing an instance of A into B, but $var is a static Member of A which can only be accessed using :: like A::$var.
private function f() {
echo A::$var;
}
EDIT:
if you want to make it dependant of the instance stored in $a you could do something like:
public function f() {
$class = get_class($this->a);
echo $class::$var;
}
First thing I'm seeing is that you couldn't call f() as it is private.
$b->f();
Then maybe you can turn to something like this?
class B extends A{
private $a;
public function __construct($a) {
$this->a = $a;
}
public function f() {
echo static::$var;
}
}
The following will work but I'd question why you'd want to do this:
class A {
public static $var = 'foo';
}
class B {
private $a;
public function __construct($a) {
$this->a = $a;
}
public function f() {
if ($this->a instanceof A) {
echo A::$var;
}
}
}
$b = new B(new A());
$b->f();
you don't need to do anything specifically with $this->a as $var is static to the class.
You could write a function like so:
function get_static_property($class, $property) {
return $class::$$property;
}
I can't help but wonder why you would need this. Any reason why you can't have A implement an interface? Or use a trait?
That way you can just echo $this->a->someMethodName().
PS. returning strings is generally preferred over echoing inside methods.
I'm a totally newbie in class writing in PHP, started a couple days ago.
I'd like to declare a new "public property" inside a method to be used in other methods.
This is what I thought (Of course it doesnt' work!):
class hello {
public function b() {
public $c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $c;
}
}
$new = new hello;
$new->output();
Thanks in advance for any tips.
I'd like to declare a new "public property" inside a method to be used in other methods.
If the other methods are part of the same class, you don't need a public property, a private property will fit your needs. Private properties are accessible within the same class only which helps to keep things simple.
Also understand the difference between declaring a property and assigning a value to it. Declaring is done when the code is loaded, assigning when it executes. So declaring (or defining) a property (private or public) needs a special place in the PHP syntax, that is in the body of your class and not inside in a function.
You access properties inside the class by using the special variable $this in PHP.
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs [to]). From PHP Manual
Private property example:
class hello {
private $c; # properties defined like this have the value NULL by default
public function b() {
$this->c = 20; # assign the value 20 to private property $c
}
public function output() {
echo $this->c; # access private property $c
}
}
$new = new hello;
$new->output(); # NULL
$new->b();
$new->output(); # 20
Hope this is helpful. You use a private property because everything else in your program does not need to care about it, so inside your class you know that nothing else can manipulate the value. See as well VisibilityDocs.
Every variable of the class is public when you define it inside of a method (function)! You can do this the following way:
class hello {
public function b() {
$this->c = 20;
}
public function output() {
echo $this->c;
}
}
$new = new hello;
$new->output();
or let function b() return $c and then pass it as a variable to output():
class hello {
public function b() {
return $c = 20;
}
public function output($c) {
echo $c;
}
}
$new = new hello;
$c = $new->b();
$new->output($c);
Remember all variables inside a function are ONLY accessable from within that particular function...unless you use $this of course, which makes the variable a class property!
Also, it's recommended to only return variables...echo is only for the real output, the pure HTML, the template, your view, if you know what I mean :)
Try this instead:
class hello {
public $c = null;
public function b() {
$this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $this->c;
}
}
class hello {
public $c;
public function b() {
$this->c = 20;
}
public function output() {
$this->c;
}
}
Note: If you need to use a property in another method you don't need to declare that method as public, you should declare the property as private, and you'll have access to your property with no problem:
class hello {
private $c;
public function b() {
$this->c = 20;
}
public function output() {
$this->c;
}
}
class hello {
public $c;
public function b() {
$this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
return $this->c;
}
}
$new = new hello;
echo $new->output();
class hello {
var $c;
function __construct() { // __construct is called when creating a new instance
$this->c = 20;
}
public function getC() {
return $this->c;
// beter to return a value this way you can later decide what to do with the value without modifying the class
}
}
$new = new hello; // create new instance and thus call __construct().
echo $new->getC(); // echo the value
Rewrite this code like this :-
class hello {
public $c;
public function b() {
public $this->c = 20; //I'd like to make $c usable by the method output()
}
public function output() {
echo $this->c;
}
}
$new = new hello;
$new->output();
I want to understand what is the difference between these chaining method
$obj->prop_a()->prob_b($y);
and
$obj->prop_a->prob_b($x);
how to define it and how it works.
Thanks in advance
On
$obj->prop_a()->prob_b($y);
you access prop_a() as a function. It return (obviously) an object, which implements an prob_b()-function.
On
$obj->prop_a->prob_b($x);
you access prop_a as a public property which contains an object, which again implements an prob_b() function.
One calls prop_a as a method, the other doesn't.
This:
class A {
public function prop_b($x) {
return 4*$x;
}
}
class B {
public function prop_a() {
$a = new A();
return $a;
}
}
class C {
public $prop_a;
public function __construct() {
$this->prop_a = new A();
}
}
$b = new B();
$c = new C();
$b->prop_a()->prob_b(5);
$c->prop_a->prop_b(5);