I am coming from the Ruby world and I have a PHP project I currently work on.
Like in Ruby scripts, is it possible to declare class methods in PHP? Basically, I'm asking what the equivalent of the following code would be in PHP
class A
def hello; "hello from object"; end
def self.hello; "hello from class"; end
end
Note the difference between the instance method and the class method.
In PHP class methods are usually referred to as static methods and are declared as such. To directly translate your example:-
class A
{
public function hello()
{
return "hello from object";
}
//We can't have two methods with the same name.
public static function s_hello()
{
return "hello from class";
}
}
Then you would call the methods like this:-
//For a static method we don't need an instance
echo A::s_hello;
//But we do for an instance method
$a = new A();
echo $a->hello();
You can also have static properties, so the above example could be modified something like this:-
class A
{
private static $s_hello = "hello from class";
private $hello = "hello from object";
public function hello()
{
return $this->hello;
}
//We can't have two methods with the same name.
public static function s_hello()
{
return self::$hello;
}
}
http://php.net/manual/en/language.oop5.static.php
class A
def hello; "hello from object"; end
def self.hello; "hello from class"; end
end
class A {
// attributes or properties
public $age;
private $gender;
protected $location;
// needs to be static to be called as self:: inside the class
public static function hello(){
return "hello from object";
}
// use this keyword to be called inside the class
public function hello1(){
return "hello from object";
}
public function hello2(){
print(self::hello());
print(this->hello1());
}
// How about private method
private function hello3(){
return "hello world";
}
}
Calling Outside the class
$instance = new A();
//static
$instance::hello();
//non static
$instance->hello1();
$instance->hello2();
Look at static methods: http://php.net/static
A static method is called like that:
Foo::aStaticMethod();
There is no way in php to have multiple definition of the same function in single class
rather you can do this
abstract class A{
static function hello()
{
return 'hello';
}
}
class B extends class A
{
function hello()
{
return 'hello from Object';
}
}
A::hello();
$t = new B();
$t->hello();
Related
I have a base class with a static function. But I would like to have a way to know the actual class (could be the base class or a derived class) within whose context I am calling the static function.
For example:
<?php
class Foo
{
static function Test()
{
$c = self::class;
echo "Hello, I am creating a new instance of type $c";
return new $c;
}
}
class Bar extends Foo
{
public $someProperty;
}
$b = Bar::Test(); // This should do something different than Foo::Test();
?>
Note that the self::class in the Test() function always results in 'Foo' even if I'm calling it using the Bar:: context.
I understand I could override the Test() function in Bar but that's not what I want, I want to keep the implemented functionality in the base Test() function. But just with the actual static class context that I'm calling it with.
Is there a way for the above Test() function to say "I am creating a new instance of type Bar" and return a Bar instance, rather than a Foo ?
Let me introduced you to late static binding.
Consider the following code, it's not exactly like yours but it highlight's the issue I believe you are facing.
<?php
class A
{
public static $string = 'I am from class A';
public static function getString()
{
return self::$string;
}
}
class B extends A
{
public static $string = 'I am from class B';
}
B::getString(); // returns 'I am from class A' ???!
?>
To get around this you can use late static binding to use the variable at run time context (rather than at compile time context)
<?php
class A
{
public static $string = 'I am from class A';
public static function getString()
{
return static::$string; // note the change here
}
}
class B extends A
{
public static $string = 'I am from class B';
}
B::getString(); // returns 'I am from class B' and all is well
?>
Far more information than I can give you is available here: https://www.php.net/manual/en/language.oop5.late-static-bindings.php
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 code something like this to give you an example
This is using "$this->"
<?php
class A{
public function example(){
echo "A";
}
}
class B extends A{
public function example2(){
$this->example();
}
}
$b = new B();
echo $b->example2();
?>
and This is using parent::
<?php
class A{
public function example(){
echo "A";
}
}
class B extends A{
public function example2(){
parent::example();
}
}
$b = new B();
echo $b->example2();
?>
What is different between $this-> and parent:: in OOP PHP?
The difference is that you can access a function of a base class and not of the currient implementation.
class A {
public function example() {
echo "A";
}
public function foo() {
$this->example();
}
}
class B extends A {
public function example() {
echo "B";
}
public function bar() {
parent::example();
}
}
And here some tests:
$a=new A();
$a->example(); // echos A
$a->foo(); // echos A
$b=new B();
$b->example(); // echos B
$b->foo(); // echos B
$b->bar(); // echos A
parent::example() calls the parent class method, where $this->example() call the current class method.
In your example there's no difference, since class B doesn't override example() method. It is common to write something like this (maybe it will help you to understand better this concept):
class A {
public function example(){
echo 'A';
}
}
class B extends A {
public function example(){
echo 'B';
}
public function example2(){
$this->example();
}
public function example3() {
parent::example();
}
}
$b = new B();
$b->example2();//print B
$b->example3();//print A
In simple words
$this is an instance reference, so whenever you use $this it starts referencing current class methods and properties.
parent is a parent reference which can be used to access parent class properties and methods with public or protected access modifier.
parent:: will call a method or an attribute of the parent. However, since this is refering to the class and not any kind of instance, you can only call a static method or attribute.
$this-> refers to the current instance of the object you call this in.
You could also want to refer to self:: which refers to the current class (once again, no instance involved here) within an object or a static method.
If i extend a static class in PHP, and the parent class refers to "self::", will this refer to the self in the extended class?
So, for example
<?php
Class A
{
static $var
public static function guess(){self::$var = rand(); return $var}
}
Class B extends Class A
{
public static function getVar(){return self::$var}
}
If I ran
B::guess();
then B::getVar();
is the value for Var stored in A::$var or B::$var?
Thank you.
Late static binding was introduced in PHP 5.3, it allows you to control this behavior.
It's easy to test:
class ClassA {
public static function test(){ self::getVar(); }
public static function getVar(){ echo 'A'; }
}
class ClassB extends ClassA {
public static function getVar(){ echo 'B'; }
}
ClassA::test(); // prints 'A'
ClassB::test(); // also prints 'A'
... hope that helps :)
Additional information, usage of self or $this is different into extended classes
class ClassA {
public function test(){ self::getVar(); }
public function test2(){ $this->getVar(); }
public function getVar(){ echo 'A'; }
}
class ClassB extends ClassA {
public function getVar(){ echo 'B'; }
}
$classB = new ClassB();
$classB->test(); // prints 'A'
$classB->test2(); // prints 'B'
I'm just starting with Object Oriented PHP and I have the following issue:
I have a class that contains a function that contains a certain script. I need to call a variable located in that script within another function further down the same class.
For example:
class helloWorld {
function sayHello() {
echo "Hello";
$var = "World";
}
function sayWorld() {
echo $var;
}
}
in the above example I want to call $var which is a variable that was defined inside a previous function. This doesn't work though, so how can I do this?
you should create the var in the class, not in the function, because when the function end the variable will be unset (due to function termination)...
class helloWorld {
private $var;
function sayHello() {
echo "Hello";
$this->var = "World";
}
function sayWorld() {
echo $this->var;
}
}
?>
If you declare the Variable as public, it's accessible directly by all the others classes, whereas if you declare the variable as private, it's accessible only in the same class..
<?php
Class First {
private $a;
public $b;
public function create(){
$this->a=1; //no problem
$thia->b=2; //no problem
}
public function geta(){
return $this->a;
}
private function getb(){
return $this->b;
}
}
Class Second{
function test(){
$a=new First; //create object $a that is a First Class.
$a->create(); // call the public function create..
echo $a->b; //ok in the class the var is public and it's accessible by everywhere
echo $a->a; //problem in hte class the var is private
echo $a->geta(); //ok the A value from class is get through the public function, the value $a in the class is not dicrectly accessible
echo $a->getb(); //error the getb function is private and it's accessible only from inside the class
}
}
?>
Make $var a class variable:
class HelloWorld {
var $var;
function sayHello() {
echo "Hello";
$this->var = "World";
}
function sayWorld() {
echo $this->var;
}
}
I would avoid making it a global, unless a lot of other code needs to access it; if it's just something that's to be used within the same class, then that's the perfect candidate for a class member.
If your sayHello() method was subsequently calling sayWorld(), then an alternative would be to pass the argument to that method.