I want to know is it possible to have a class that's extended have a var set and used from the base class?
eg:
class me
{
public $hello = array();
protected function setter($me)
{
$this->hello[] = $me;
}
}
class foo extends me
{
public function __construct()
{
$this->setter('foo');
}
}
class yoo extends me
{
public function __construct()
{
parent::setter('yoo');
}
}
$me = new me();
$foo = new foo();
$yoo = new yoo();
print_r($me->hello);
the array printed is array() nothing is set.
Yes, you can do this by making $hello static:
public static $hello = array();
In doing so, you will have to drop the $this from $this->hello[] = $me; and replace it with a self, as hello will not longer be unique to the current object instance:
self::$hello[] = $me;
You were using
parent::setter('yoo');
But in parent class me, that function is not defined as static. So you cannot use :: to call un-static function.
Related
I have a code structure like this:
class myclass{
use App/classes/Log
public function myfunc1 () {
$log_obj = new Log;
$log_obj->log('something1');
}
public function myfunc2 () {
$log_obj = new Log;
$log_obj->log('something2');
}
public function myfunc3 () {
$log_obj = new Log;
$log_obj->log('something3');
}
}
In reality, I have 12 methods which I need to make a object of Log class in the most of them. Now I want to know, isn't there any better approach to I do that (making an object) once? For example using a static property and setting the object to it or whatever ..
You can assign the Log instance to a property of your myclass using __construct. Here's an example of accessing a method of a class inside another class:
class Test {
public $var = 'test';
public function show_var() {
echo $this->var;
}
}
class Test_2 {
protected $test;
public function __construct() {
$this->test = new Test;
}
public function show_test() {
$this->test->show_var();
}
}
$test_2 = new Test_2;
$test_2->show_test();
See here in action.
How can I access to class variable from outside without creating new instance in PHP ? Something like this:
class foo
{
public $bar;
}
echo foo::$bar;
Is it possible or I must create method that will print or return this value and use it or create new instance ( $a = new foo; echo $a->$bar ) ?
EDIT: I don't want to create constant but classic variable that will be changed later.
make this variable static to access it with out class object.
If you wanted to change static variable value by method then you need to use static method
you can try like this:
class foo
{
public static $bar ="google";
public static function changeVal($val){
self::$bar=$val;
}
}
foo::changeVal("changed :)");
echo foo::$bar;
Output : changed :)
Demo : https://eval.in/107138
You also can changed it like this without static method:
foo::$bar = "changed";
demo : https://eval.in/107139
like this:
class foo
{
public static $bar ="google";
}
echo foo::$bar;
Output: google
demo: https://eval.in/107126
IF it makes sense to use a static variable:
class foo{
public static $bar = 'example';
}
Which could be accessed like so:
echo foo::$bar;
If the value will never change during runtime, then you probably want a class constant (http://www.php.net/oop5.constants)
class foo {
const bar = 'abc';
}
...otherwise you want a public static variable (http://www.php.net/manual/en/language.oop5.static.php)
class foo {
public static $bar = 'abc';
}
...either way, access it like this
echo foo::bar;
You can access the class variable without creating instances only when the variable is markesd as static:
class foo
{
public static $bar;
}
This is how you use a class variable:
// with instantiation
class foo {
// initialize on declare
public $bar = 1;
// or initialize in __construct()
public function __construct(){
$this->bar = 1;
}
}
$foo = new foo();
var_dump($foo->bar);
// static way
class static_foo {
public static $bar = 1;
}
var_dump(static_foo::$bar);
And this is how you instantiate a class from a random class name string variable.
$foo = new foo();
$random_class_name = $foo->bar;
try {
// following line throws if class is not found
$rc = new \ReflectionClass($random_class_name);
$obj = $rc->newInstance();
// can be used with dynamic arguments
// $obj = $rc->newInstance(...);
// $obj = $rc->newInstanceArgs(array(...));
} catch(\Exception $Ex){
$obj = null;
}
if($obj){
// you have a dynamic object
}
What's your actual question?
I have a class:
class My_Class {
private $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
How do I access $playlist_table_name from markup.php file?
I tried using: $this->playlist_table_name, but I get:
Using $this when not in object context
If you want to access the variable like that, you will need to mark it as public
class My_Class {
public $playlist_table_name;
public function __construct() {
$this->playlist_table_name = "something";
require_once('markup.php');
}
}
You are then going to want to instantiate the class before attempting to use it.
$MyClass = new My_Class;
echo $MyClass->playlist_table_name;
That will allow you to echo out the value.
OK, I'm having a bit of a problem. Here's the scenario:
I need to be able to get the constructor of test2 to be able to access the class property test that is inside main_class that has been set by main_class' constructor. I'm not sure how to get it to work, and I need the system to work like exactly like this. Now, this WOULD work if I set the class variable in the code, like this var test = "hello"; in the class definition, but of course in this case, main_class::test is set by it's constructor and is not a "var", so it doesn't work.
Here is a highly simplified version of my code:
index.php:
<?php
class main_class
{
private $test2;
public function __construct()
{
$this->test2 = array();
include("./test1.php");
$var_name = "test";
$this->$var_name = new test1();
}
protected function do_include()
{
include("./test2.php");
$this->test2["test2"] = new test2();
}
}
$test = new main_class();
?>
test1.php:
class test1 extends main_class
{
public function __construct()
{
$this->do_include();
}
}
?>
test2.php:
class test2 extends test1
{
public function __construct()
{
print_r($this->test);
}
}
?>
With this code, I get this error:
Notice: Undefined property: test2::$test
Thanks in advance...
I suspect that part of the problem may be that you're not calling the parent constructor in your test2 class:
class test2 extends test1
{
public function __construct()
{
parent::__construct();
print_r($this->test);
}
}
If that line is left out, then your test2 constructor overrides the test1 constructor completely, and $this->do_include() is never called.
Also, remember that when you call $this->test2["test2"] = new test2();, you are creating a new instance of this class, which is not associated with the current one.
Just to clarify, here's the order of events:
$test = new main_class(); // calls the constructor of main_class:
public function __construct()
{
$this->test2 = array();
include("./test1.php");
$var_name = "test";
$this->$var_name = new test1();
}
Then:
$this->$var_name = new test1(); // calls the constructor of test1:
public function __construct()
{
$this->do_include();
}
...which calls do_include() from main_class:
protected function do_include()
{
include("./test2.php");
$this->test2["test2"] = new test2();
}
Then:
$this->test2["test2"] = new test2(); // calls the constructor of test2:
public function __construct()
{
print_r($this->test);
}
This creates a new object, and all you're doing in its constructor is printing a variable ($test) that does not exist yet...because you haven't done anything to create it.
I have code similar to the following:
class ModuleRaceRegistration extends Module
{
protected $strTemplate = "template";
protected function compile()
{
// this doesn't work
$this->strTemplate = "template2";
}
}
From within the compile function I need to change the $strTemplate member. How can I do this?
Is an error being returned? Also, this might not be the case but compile is a protected method so you can only call it from within the class. If you are trying to call it from outside of the class, then it would need to be public.
Let me try
Example from manual
<?php
abstract class Base {
abstract protected function _test();
}
class Bar extends Base {
protected function _test() { }
public function TestFoo() {
$c = new Foo();
$c->_test();
}
}
class Foo extends Base {
protected function _test() {
echo 'Foo';
}
}
$bar = new Bar();
$bar->TestFoo(); // result: Foo
?>