Php get variable from required file - php

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.

Related

How can I make an object once for all methods?

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.

Acces variable set in parent constructor from child function

There are already many threads about this kind of problem, but for some reason i can't get it to work.
In TestClass::test(), $db is NULL.
The $db value is set in App construct and I'm trying to recover that value from an extended class function. (so i don't need to set $db everytime everywhere).
Some help would be greatly appreciated, thanks.
File : index.php
<?php
include('classes/App.class.php');
$oApp = new App();
echo TestClass::test();
?>
File : App.class.php
<?php
class App {
protected $db;
public function __construct () {
include_once("CAutoLoader.class.php");
$oCAutoLoader = new CAutoLoader();
$this->db = "someValue";
}
}
?>
File : TestClass.class.php
<?php
class TestClass extends App
{
function __construct () {
}
public static function test () {
return $db;
}
}
?>
File : CAutoLoader.class.php
<?php
class CAutoLoader {
CONST CLASS_EXTENSION = '.class.php';
public function __construct () {
spl_autoload_register(array($this, 'loader'));
}
private function loader ($className) {
include $className . self::CLASS_EXTENSION;
}
}
?>
You forgot a this in your TestClass and a static method cannot access non-static properties. Remove the static keyword and return the right value.
public function test() {
return $this->db;
}
Edit:
If you meant to retrieve the instance of the db via a static method you must declare the variable as static.
class App {
protected static $db = 'hey';
...
}
class TestCase extends App {
public static function test() {
return parent::$db;
}
}
echo TestCase::test(); // returns hey

How do I Instantiate a class within a class in .php

I'm new to OOP and I can't figure out why this isn't working. Is it not ok to instantiate a class with in a class. I've tried this with included file with in the method and it didn't make a change.
include('Activate.php');
class First {
function __construct() {
$this->activate();
}
private function activate() {
$go = new Activate('Approved');
}
}
$run = new First();
Are you saying that you want to access $go? because, if that is the case, you need to change the scope of it.
As you see, $go in this method is only available inside activate():
private function activate() {
$go = new Activate('Approved');
}
to make it reachable from other location within the class, you would need to declare it outside activate():
private $go = null;
you call $go by using $this:
private function activate() {
$this->go = new Activate('Approved');
}
After that, if you want to access go from outside class, you would need to create wrapper:
public function getGo(){
return $this->go;
}
Hope this helped. Also, you can read the documentation about OOP in PHP.
Okay I figured out my issue. My code had two errors. One that being my method was set to private and secondly I had an error in my $parms that I was sending to the class.
include('Activate.php');
class First {
function __construct() {
$this->activate();
}
private function activate() {
$go = new Activate('Approved');
}
}
$run = new First();
You can access your private method with the help of create another public function in the same class and call the private function in the public method or function. Now you can call your private function with the help of $run instance.
For example:
class Cars{
function getting(){
echo ("Hello World");
}
private function getting2(){
echo ("Hello World Againg");
}
public function getting3(){
$this->getting2();
}
}
$class_object=new Cars;
print($class_object->getting3());
Your output screen like the below image.

Calling static class member in PHP

I am trying to get a variable from a php class without having to use "new classname()"
This is my code:
class myVars {
static $varx = null;
public function __construct() {
$this->varx = "test";
}
}
echo myVars::$varx;
I also tried replacing $this-> with self::, but nothing gets printed. How should I code the class in order to call myVars::$varx?
The problem you are having, is that you are not instantiating an object, so the constructor never gets called.
What you could do is:
class myVars {
static $varx = null;
public function __construct() {
$this->varx = "test";
}
}
myVars::$varx = "test";
echo myVars::$varx;
Or you could create an object and have the constructor change the static variable.
This should make your static variable publicly accessible.
class myVars {
public static $varx = null;
public function __construct() {
self::$varx = "test";
}
}
echo myVars::$varx;
However, in your example, the constructor is never called, so the modification to the static variable is never made.

How do I use objects and access their methods while within an object in PHP?

How do I use an object (along with its methods and properties) when I'm inside an object?
Say I have useless classes like these:
class Fruit {
private $name; // Name of the fruit.
private $health = 10; // 0 is eaten, 10 is uneaten.
private $object; // This is a PHP object.
public function __construct($name) {
$this->name = $name;
}
public function set($varname,$value) {
$this->$varname = $value;
}
}
class Eater {
private $name;
public function eat($object) {
$object->set('health',0); // I know I can pass and modify objects like this.
// The object is passed by reference in PHP5 (but not 4), right?
}
}
And I use it as such:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->eat($apple);
?>
But if I modify the Eater class like so:
class Eater {
private $name;
private $objectToEat; // Let's say if I need the object to be over here instead of in a method.
public function set($varname,$value) {
$this->$varname = $value;
}
public function eat() {
$this->objectToEat->set('health',0); // This doesn't work!
}
}
And set the main program like so:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->set('objectToEat',$apple);
$paul->eat();
?>
How can I access the object's properties from inside a method? I know I use $this->objectToEat to tell PHP I'm talking about the class properity, but since that property is an object, how do I access the object's methods?
I've tried $this->objectToEat->set('health',0) but that doesn't work. I hope you guys understand what I'm trying to get at (sorry, I can't figure out how to condense my question without compromising clarity)!
You have to set the property correctly. Since it's private, you can't do this from outside the object, so you have to use encapsulation:
class Eaters {
private $name;
private $objectToEat;
public function eat() {
$this->objectToEat->set('health',0); // Assumed "object" was just a typo
}
public function setObjectToEat($object) {
$this->objectToEat = $object;
}
}
Then use it like so:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->setObjectToEat($apple);
$paul->eat();
?>
Note: In this brief example, your original method is a better design. In certain cases, you might want to prime the method to be used by setting properties beforehand, but more often you want to call it with parameters directly, since it's more clear and more reusable (compartmentalized).
This answer modifies Renesis' answer
In the class, the object to eat is a private variable hence you can't go
$paul->objectToEat = $apple;
What you can do is to make a setter method inside Eaters
class Eaters {
private $name;
private $objectToEat;
public function eat() {
$this->objectToEat->set('health',0); // Assumed "object" was just a typo
}
public function setFood($object) {
$this->objectToEat = $object;
}
}
Therefore, you can call the setFood() method instead.
OR
Change eat() to
public function eat($object) {
$this->object->set('health',0);
return $object;
}
Saving the modified object back to the original variable.
OR
class Eaters {
private $name;
public function eat(&$object) { // this passes object by reference
$object->set('health', 0);
}
}
Although this code is not tested, that is how you can pass a variable by reference.
NOTE: You only need the & when defining the method not when you're passing an argument. For more info about Passing by Reference go to this link
It's probably because your eat method isn't accepting any parameters, and the Eaters class has no $object property.
Can you make $objectToEat a reference and then use it as such in the eat() function?
you have to set $this->object in class Eaters
function __construct($object){
$this->object = $object;
}
or
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->eat($apple);
?>
class Tester {
private $variable;
private $anObj;
public function testFn($val) {
$this->variable = $val;
$this->anObj = new SecondObj();
$this->doSomething();
}
public function doSomething() {
echo("My variable is set to " . $this->variable);
$this->anObj->wow();
}
}
class SecondObj {
public function __construct() {
echo("I'm new!");
}
public function wow() { echo("Wow!"); }
}
$tester = new Tester();
$tester->testFn(42);
Output:
I'm new!My variable is set to 42Wow!

Categories