I have a class that look like this
class a {
private $one;
private function abc() {
$this->one = "I am a string";
return $this;
}
$call = new a;
$call->abc()->other_function();
As I was doing matutor method, php has caught a fatal error on calling function abc(). It said Call to private method xxx from context.
What I know of oop is very new, that private method/property can only be used within the same class.However, I cannot call the abc() even it is within the same class.
How come?
Because you are not calling the method inside the class you are doing so outside the class code.
$call = new a;
$call->abc()->other_function();
this is outside the context of the class, and this is why you get a Fatal Error.
Private can only be used in the class itself.
Protected can only be used in the class itself and child classes.
Public can be used anywhere.
class a {
private $one;
public function abc() { //notice the public
$this->one = "I am a string";
return $this->one;
}
}
$call = new a;
echo $call->abc();
Related
I have a variable that holds a class name.
public $modelClass = 'common\models\Notecard';
That class has a static method.
public static function do_something() { ... }
Given this information, I would like to call the static function. For non-static functions, I can do the following:
$model_name = $this->modelClass;
$model = new $model_name();
$model->do_something_else();
Yes, that is pretty easy:
You can either just call the function from your instance e.g.
$model_name = $this->modelClass;
$model = new $model_name();
$model::do_something();
or using call_user_func()
call_user_func([$modelClass, 'do_something']);
You can use $model_name::do_something_else().
class foo {
public static function bar() {
echo "Called bar";
}
}
$fname = "foo";
$fname::bar();
Outputs Called bar
Works on php7.
<?php
class foo
{
private $callVar = false;
private $constructorVar = false;
public function __construct()
{
$this->constructorVar = get_class($this);
}
public function __call($methodName, $arguments)
{
$this->callVar = get_class($this);
call_user_func_array(array($this, $methodName), $arguments);
}
protected function method($params)
{
echo('<br>Call var: '.$this->callVar.'<br>Constructor var: '.$this->constructorVar);
}
}
class foo_sec extends foo
{
protected function method($params)
{
echo 'First call: ';
parent::method($params);
echo '<br> Second call: ';
$test = new foo_trd();
$test->method('param2');
}
}
class foo_trd extends foo
{
}
$m = new foo_sec();
$m->method('param');
?>
Gives this result:
First call:
Call var: foo_sec
Constructor var: foo_sec
Second call:
Call var:
Constructor var: foo_trd
This code should return the name of the instance that is currently running, but if the instance is inside another instance, the class name is empty in second one. Is this a PHP bug or some other issue I'm missing?
In other words, the Call var in the second call should be foo_trd - not empty.
I believe you are experiencing something interesting to do with PHP and its inheritance visibility.
In short when you are inside and instance of Foo you have access to the protected and private scope of all other instances that also extend Foo. As you are allowed to access the protected method() directly you will skip the call to __call().
This can actually be useful when you want to make your constructor private and enforce factories.
I have the following class, and for some reason it's not accessing the test property. Why is this? I'm new to OOP, so please be easy on me. Thanks
class Test {
private $test;
function __contruct() {
$this->test = "test";
}
static function test() {
echo $this->test;
}
}
$test = new Test;
$test::test();
Because static methods are callable without an instance of the object
created, the pseudo-variable $this is not available inside the method
declared as static.
PHP Documentations.
Good morning.
It seems you have 3 issues with your code.
There is a syntax error at constructor line change it from __contruct to __construct.
Change test function visibility to public
Access your function with the -> instead of ::
To elaborate further on the above answers: Static methods and variables are not linked to any particular instance of the object, this is why you have to call test with $test::test(). This also means that you cannot access an instance variable from without a static method and it doesn't really make sense to do so (If you had multiple instances of the object with different values set for that variable, how would the interpreter know which instance/value to use?)
If you want to have a field accessible from a static method then you have to make the field static as well. So, if you wanted to have $test accessible from your static method test() then you'd have to write your function as something along these lines:
class Test {
private static $test;
function __contruct() {
Test::$test = "test";
}
public function test() {
echo Test::$test;
}
}
$test = new Test;
$test::test();
However, it doesn't really make sense to be initialising a static field like that in your constructor. So you'd more likely be wanting to do something like:
class Test {
private static $test = "test";
function __contruct() {
}
public static function test() {
echo Test::$test;
}
}
$test = new Test;
$test::test();
Or, if you don't actually require test() to be static then you could just make it an instance method:
class Test {
private $test = "test";
function __contruct() {
$this->$test = "test"
}
public function test() {
echo $this->$test;
}
}
$test = new Test;
$test->test();
I am new to php and trying to call a function in another class.
How do I call function1 and function2 in class xyz???
class abc {
private $lmn = "lmn";
private $say1;
private static $static;
private function __construct(){
$say1 = print $this->lmn;
}
public static function1(){
$static = "YEAAHHHH";
}
public function function2(){
return $this->say1;
}
file 2:
require 'abc.php';
class xyz {
/**
* $e = new xyz();
*
*/
$e = xyz:: function1();// error
$d = xyz:: function 2(); //error
}
Also under what circumstance I should use
$obj = new class();
$obj->functionname();
and
$obj = class::functionname();
You have 2 different types of methods here, static and non-static.
To call the static (function1())
You don't need to instantiate the class, as it's static.
class zyx {
public function foo() {
return abc::function1();
}
}
To call the non-static (function2())
You need to instantiate the class, as it's not static.
class zyx {
public function foo() {
$abc = new abc();
return $abc->function2();
}
}
You would call function1 like:
abc::function1();
It is a method in abc not xyz.
function2() you would only call if you had an instance of abc because it is an instance method and not a static method. I.e.
$abc = new abc();
$abc->function2();
Static functions are intended to be called on classes, instance methods (i.e. function2() are intended to be called on instances of classes. I would recommend reading http://php.net/manual/en/oop5.intro.php.
Static functions can be called without instantiating your class...
$myClass::function1();
Non-static functions need to be instantiated first:
$myClass = new abc();
$myClass->function2();
So in your example:
require 'abc.php';
class xyz {
public function CallFunc1()
{
abc::function1();
}
public function CallFunc2()
{
$myClass = new abc();
$myClass->function2();
}
}
require 'abc.php';
class xyz {
public function static(){
return abc:: function1();// this is a static function
}
public function nonstatic(){
$e = new abc();
return $e->function2();
}
}
First of all, you can't have a space between function and 2():
$d = xyz::function2(); //correct
$d = xyz::function 2(); //very incorrect
I was going to have a second of all, but #hd beat me to it.
I think you have messed up with the code. I can share something with something that I found very helpful as an answer for your second question. Spend a bit time reading this. This is very basic, simple and well guided.
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
<?php
class Foo {
private function FooFunction(){
}
}
class Bar extends Foo {
public function BarFunction(){
$this->FooFunction();
}
}
$foo = new Foo();
$foo->FooFunction(); //Fatal error: Call to private method Foo::FooFunction()
//(Fair enough)
$bar = new Bar();
$bar->BarFunction(); //Fatal error: Call to private method Foo::FooFunction()
//from context 'Bar'
I'm having some difficulty understanding how to properly declare functions in a class which can then be used in an extension of that class
When I instantiate Foo I'd like FooFunction to remain private.
However, I do need to be able to call it from within Bar.
change code as below:
<?php
class Foo {
protected function FooFunction(){
}
}
class Bar extends Foo {
public function BarFunction(){
$this->FooFunction();
}
}
private methods not accessable in child class.
you need to use protected method type.