Pulling variables in PHP across files into classes - php

I have the following:
config.php
class myObject {
public $_access_token;
public function __construct() {
$this->_access_token = '';
}
}
I need to pull the _access_token. Is this the best way to do it?
index.php:
require("config.php");
class gotime
{
public function getAccessToken(){
$obj = new myObject();
return $obj->_access_token;
}
Can I create the class outside the public function? It seems so inefficient to be putting a class creation in every function. I have another 12 variables I need to pull across files and I would like to set them in one place.

You can also use them like this.
class myObject
{
public static $_access_token;
public function __construct()
{
self::$_access_token = 'some value';//set the value you want
}
}
echo myObject::$_access_token;

Related

Possible for instantiated sub class to access property set by parent class?

I'm new to object-oriented PHP, and still wrapping my head around classes and subclasses.
In this simple example, $this->siteName will be empty inside the instantiated class Two (called from class One's method "build").
Is there any way for an instance of class Two to inherit that property value set by the parent?
I know I could just turn class Two into a method of class One, but I like having separate files for organization.
Is there a better way to do this?
File One
class One {
protected $siteName;
function __construct() {
$this->siteName = 'Example';
}
public function build() {
$two = new Two();
return $two->build();
}
}
File Two
class Two extends One {
public function build() {
return "<h1>$this->siteName</h1>";
}
}
By the sound of it you're trying to solve the wrong problem.
My understanding of your question is that you basically want to have the code for public function build() in another file for organisation reasons.
Here's how:
class One {
private $siteName;
public function __construct() {
$this->siteName = 'Example';
}
use One_build;
}
Assuming you have the proper autoloading setup, or have included the files correctly, you can then have your separate file:
trait One_build {
public function build() {
return "<h1>".$this->siteName."</h1>";
}
}
This allows for a horizontal spreading of your source code.
Your code does work with a few tweaks. You need to concatenate your string's h1 tags with your variable, quotes can't be around the entire thing.
Other than that, just create an instance of the class and trying it out.
class One {
protected $siteName;
function __construct() {
$this->siteName = 'Example';
}
public function build() {
$two = new Two();
return $two->build();
}
}
class Two extends One {
public function build() {
return "<h1>".$this->siteName."</h1>";
}
}
$taco = new Two();
echo $taco->build();
This will return <h1>Example</h1>

PHP how to call function from another class?

I have a class Database in database.php with 145 functions(17,000 lines) and now i've read this is bad practice, so i want sort out the functions correctly into specific classes rather than a "God" Class.
What i want to know is how do i call a function from another class? Below is an example; How do i call function two from within function one?
database.php
require("connect.php");
class Database {
private $connect;
function one() {
//call function two
}
}
forms.php
require("connect.php");
class Forms {
private $connect;
function two() {
//returns forms
}
}
How do i do this?
In the example you gave you would do:
function one() {
$forms = new Forms;
$forms->two();
}
Another option would be
function one() {
Forms::two();
}
And in Forms you would change the method to:
static function two() {
}

Php get variable from required file

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.

Variables in __construct() when called by mysqli_fetch_object

I've got a class 'Event' which I am creating objects from via mysqli_fetch_object. The __construct() function is running and the objects variables are being set but they aren't set within the __construct() function.
I am using the following line to create the object:
$events[$x] = $result->fetch_object("Event")
When I run the following function by calling $events[$x]->eventPlaces(); it echos the variable.
public function eventPlaces()
{
echo $this->capacity;
}
However with the same code in the construct function it echos nothing.
public function __construct()
{
echo $capacity;
echo $this->capacity;
}
Apologies if I have explained this poorly, I've just got back into coding and OO php is new to me, if I missed anything then let me know.
I think you want your class to look like this:
class Event{
protected $capacity;
public __construct($capacity){
$this->capacity = $capacity;
}
public function eventPlaces(){
return $this->capacity;
}
}
Then you would do this:
$events[$x] = $result->fetch_object("Event", array(12));
echo $events[$x]->eventPlaces();

How to make a globally accessible object

Hi i have a little collection of classes some of which should be globally accessible.
I found something similar in Zend_Registry, but reading its code i cant understand how a call to a static function could return an initialized instance of a class...
i need to do something like:
<?php
//index.php
$obj = new myUsefulObject();
$obj->loadCfg("myFile.xml");
$req = new HTTPRequest();
$req->filter("blablabla");
myappp::registerClass("object",$obj);
myappp::registerClass("request",$req);
$c = new Controller();
$c->execute();
?>
Here i have filtered the Request object and i want the controller to be able to reach that already filtered request.
<?php
class Controller
{
function __construct()
{
$this->request = Application::getResource("request");//This must be the filtered var =(
}
}
?>
I don't know how to implement that Application::getResource(), the only thing i know is that it must be a static method because it can't be related to a specific instance.
Aside from static methods, PHP also has static properties: properties that are local to the class. This can be used to implement singletons, or indeed a Registry:
class Registry {
private static $_registry;
public static function registerResource($key, $object)
{
self::$_registry[$key] = $object;
}
public static function getResource($key) {
if(!isset(self::$_registry[$key]))
throw InvalidArgumentException("Key $key is not available in the registry");
return self::$_registry[$key];
}
}
1: You can acess global variables with the global keyword:
$myVar = new SomethingProvider();
class MyClass {
public function __construct() {
global $myVar;
$myVar->doSomething();
}
}
2: You can do the same using the $GLOBALS super-global:
$myVar = new SomethingProvider();
class MyClass {
public function __construct() {
$GLOBALS['myVar']->doSomething();
}
}
3: You can define a singleton class (the wikipedia has a nice example, too).
4: You could add globals as public static members (or private static members with public getters/setters) to a class:
class Constants {
const NUM_RETIES = 3;
}
if ($tries > Constants::NUM_RETRIES) {
# User failed password check too often.
}
class Globals {
public static $currentUser;
}
Globals::$currentUser = new User($userId);
I wouldn't recommend the first two methods, overwriting the values of these global variables unintentionally is too easy.
Seems to me like you might need some form of Singleton design pattern;
Check this out!
Hope it helps!

Categories