Pull variables from separate function - php

If I have two public functions and I want to pull a variable from one inside of another, what is the best way to accomplish this? I know about 'global' but this method seems like it could cause me problems down the road.
Imagine I have something like this:
class myCMS {
public function process_apples() {
$a = $_POST['$apples'];
}
public function display_apples() {
echo $a;
}
}
How would I go about using display_apples() to report $a from process_apples()? Im new to PHP, so feel free to let me know if I am violating some best practicing for organizing my code.

If you have two methods in a class, and want a variable to shared between them, you should use a class property -- which is kind of a variable that's inside a class.
Your class would then look like this :
class myCMS {
protected $a; // declare the property (won't be visible from outside the class)
public function process_apples() {
$this->a = $_POST['$apples'];
}
public function display_apples() {
echo $this->a;
}
}
And a couple of notes :
You need to use $this->property_name to access a property
Not related, but you should generally no use $_POST from inside your class : it makes your class dependent on external global variables -- of course, up to you to determine whether it's a problem or not.

You would have:
class myCMS {
private $a;
public function process_apples() {
$this->a = $_POST['$apples'];
// process
}
public function display_apples() {
echo $this->a;
}
}

Since you're using a class, you could make a private class variable, like this:
class myCMS {
private $a;
public function process_apples() {
$this->a = $_POST['$apples'];
}
public function display_apples() {
echo $this->a;
}
}

Related

Correct way of using polyporphism in php?

I'm new to object oriented php. And if there are no functions in the method testing() in the HumanClass, should i declare them as abstract?
<?php
class HumanClass
{
private $legs;
private $hands;
public function __construct($legs, $hands)
{
$this->legs = $legs;
$this->hands = $hands;
}
public function testing()
{
}
}
class StudentClass extends HumanClass
{
private $books;
public function __construct($legs, $hands, $books)
{
parent::__construct($legs, $hands);
$this->books = $books;
}
public function testing()
{
echo "StudentClass called.";
}
}
function callClass(HumanClass $c)
{
$c->testing();
}
$example = new StudentClass(4, 2, 1);
callClass($a);
?>
Is it possible to have something like this?
echo $a->testing();
instead of having another method to call testing().
Given the code that you give, it's far from clear what the testing() function is supposed to do other than just exist for you to try things. The answer to that will also determine whether the versions in the baseclass should remain there as empty function.
There are other options, too, e.g. that the derived class first invokes the baseclass (extending), or that the baseclass doesn't contain an abstract or concrete such function but only the derived one does. Which to choose is up to the informed programmer to decide.

Call external function from within class that accesses private properties

I'm trying to make my code a little drier by doing the follow in PHP:
function accessor($obj, $property) {
return $obj->$property;
}
class SomeClass {
private $variable;
function variable() {
return accessor($this, 'variable');
}
}
$some = new SomeClass;
echo $some->variable();
The above code throws an error because the external function can't access the private variable. The code is simplified, further coding would make it more useful.
I'm not sure if this is possible but it would sure be nice!
What you want are traits (PHP 5.4+)- they are practically pasted into their parent classes, so they can access private state:
trait VariableThingy {
function accessor($property) {
return $this->$property;
}
}
class Test {
use VariableThingy;
private $variable = 15;
function variable() {
return $this->accessor("variable");
}
}
But no, it's not nice. It's atrocious. And the whole code is rather pointless. If you want dry, just go with a public variable. DRY and encapsulated are usually mutually exclusive.

How do I make variable classwide accessable (Object oriented Programming PHP)

I have a class named testclass and several functions in it. They all need to access one predefined variable $need_to_have. I want to define it within the class, but how?
Example:
class testclass {
// This won't work:
private static $need_to_have = get_some_info();
public static testfunction1() {
// Do Something with $need_to_have
}
public testfunction2 () {
// Do something else with $need_to_have
}
}
Forgot to mention: I want to have it privat and I'm calling the variable only on static functions, so I can't use the constructor.
You can't do that, because you can't initialize class properties with non-constant expressions.
What you can do is put an accessor method between you and the property, e.g.:
class testclass {
private static $need_to_have;
private static $need_to_have_initialized;
public static testfunction1()
{
// Do Something with getNeedToHave()
}
private static function getNeedToHave()
{
if (!self::$need_to_have_initialized) {
self::$need_to_have = get_some_info();
self::$need_to_have_initialized = true;
}
return self::$need_to_have;
}
}
If there is a constant value that get_some_info is guaranteed to never return, you can use that to initialize $need_to_have; this will allow you to get rid of the helper property $need_to_have_initialized:
// example: get_some_info() never returns false
private static $need_to_have = false;
private static function getNeedToHave()
{
if (self::$need_to_have === false) {
self::$need_to_have = get_some_info();
}
return self::$need_to_have;
}
Another possible modification (improvement?) is to make the property (and the "initialized" flag, if applicable) local static variables inside getNeedToHave; this way they won't even be visible from anywhere within the class itself:
private static function getNeedToHave()
{
static $need_to_have = false;
if ($need_to_have === false) {
$need_to_have = get_some_info();
}
return $need_to_have;
}
This, pretty much, has a lot of ways to do it.
The most popular way here is having a so called getter (accessor) method, which will return the current information regarding the property and it still can be private.
class TestClass {
private static $myVar = 5;
public static getMyVar() {
return self::$myVar;
}
}
So, with proper autoloader, across all your application, you will be able to call
TestClass::getMyVar();
And, as you stated in the comments, nobody will have access to change it from outside the class.
However, it could be considered bad practice, not only for the static way, as also that you should not refer to a class which you have nothing coupled to it, only to retrieve information.
Best way here is to implement registry pattern, where you can register information to the global space.
Make a separated class which implements the Registry design pattern, and maybe set some constraints, so once setted your myVar nobody can reset it, on order to be able to use the following syntax
Registry::get('myVar');
This, of course, will give you the opportunity to put some more logic in that property before registering it into the globalspace, because, defining a property, might make you troubles regarding some definitions.
Make it a property of the class, set it once and use it everywhere:
class testclass
{
public static $need_to_have;
public function __construct()
{
//$this->need_to_have=1;
// Or rather the value returned by
// whatever it is you are doing with it
$this->need_to_have=$this->setNeed();
}
public function testfunction1()
{
// Do Something with $need_to_have
echo $this->need_to_have
}
public function testfunction2 ()
{
echo $this->need_to_have
}
public function setNeed()
{
$x=4;
$y=6;
$ret=$x*$y;
return $ret;
}
}
There is no (object oriented) way to "make variable classwide accessable". But it's not needed.
What you are looking for are object and class properties or class constants.
class TestClass {
const TEST_CLASS_CONSTANT = 'foo';
private static $testClassProperty;
// or with value: private static $testClassProperty = 'bar';
private $testObjectProperty;
// or with value: private $testObjectProperty = 'baz';
// This won't work
// private static $need_to_have = get_some_info();
// -- since you only can execute functions/methods outside of class structure
// or within other functions/methods.
public function testMethod() {
// Writable access to TEST_CLASS_CONSTANT: not possible, since it's a constant.
// Writable access to $testClassProperty: possible with keywords self, parent, and static.
self::$testClassProperty = 'newbar';
// Writable access to $testObjectProperty: possible with keywords this or parent.
$this->testClassProperty = 'newbaz';
// Readable access to TEST_CLASS_CONSTANT: possible with keywords self, parent, and static.
echo self::TEST_CLASS_CONSTANT;
echo PHP_EOL;
// Readable access to $testClassProperty: possible with keywords self, parent, and static.
echo self::$testClassProperty;
echo PHP_EOL;
// Readable access to $testObjectProperty: possible with keywords this or parent.
echo $this->testClassProperty;
}
}
$testObject = new TestClass();
$testObject->testMethod();

Access static variable from within a class

Simple question, is it possible to access a static variable from a $this-> call?
class testA
{
public static $var1 = "random string";
// current solution
public function getVar()
{
return self::$var1;
}
}
class testB
{
private $myObject;
public function __construct() {
$this->myObject = new testA();
// This line is the question
echo $this->myObject::var1;
// current solution
echo $this->myObject->getVar();
}
}
I'm afraid I've answered my own question. But having a few static variables I didn't want to have a function for each variable, Or even a single getVar($staticVar) when I could access it directly.
If this is the only solution. Any recommendations on a better way to implement this.
If I'm going to require a function call for each, I might as well get rid of the static variables altogether.
//method
public function staticVar1() {
return (string) 'random string';
}
You simply access the variable like this:
testA::$var1;
So using your exemple, it would be
class testB
{
private $myObject;
public function __construct() {
$this->myObject = new testA();
// This line is the question
echo testA::$var1;
// current solution
echo $this->myObject->getVar();
}
}
Try to understand the purpose of static.
static makes them accessible without needing an instantiation of the class.
They should accessed as below if the static variable is in the class
self::$var1;
below is possible in your case
testA::$var1;
would do the job here.

__get and __set magic methods not accessible

I have a class 'base' and a class 'loader', which looks like this.
class base {
protected $attributes = Array();
public $load = null;
function __construct() {
$this->load = loader::getInstance();
echo $this->load->welcome(); //prints Welcome foo
echo $this->load->name; //prints Foo
echo $this->name; //doesnt print anything and i want it to print Foo
}
public function __get($key) {
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
}
public function __set($key, $value) {
$this->attributes[$key] = $value;
}
}
class loader {
private static $m_pInstance;
private function __construct() {
$this->name = "Foo";
}
public static function getInstance() {
if (!self::$m_pInstance) {
self::$m_pInstance = new loader();
}
return self::$m_pInstance;
}
function welcome() {
return "welcome Foo";
}
}
$b = new base();
Now what I want is a way to store variables from loader class and access them from base class using $this->variablename.
How can I achieve this? I don't want to use extends. Any idea ?
I don't feel like you've fully understood what coding the OOP way means. And usually Singletons are code smells so I'll just warn you:
There's probably a better way of accomplish you goal. If you provide more informations we will help you out. In its current form the answer is the following; just remember that I higly discourage its implementation in your code.
Assuming that you want to access only public (and non static) loader's variables as this->varname in the base class you should just insert this line in the beginning of the base class constructor:
$this->attributes = get_object_vars(loader::getInstance());
This will basically initialize the attributes array with all the loader public vars so that via your __get() method you can access its value.
On a side note, take a look at Dependency Injection design pattern in order to avoid using Singletons.
Your __get/__set methods access $this->attributes but not $this->load.
You could e.g. do something like (pseudocode)
function __get($key) {
- if $attribute has an element $key->$value return $attribute[$key] else
- if $load is an object having a property $key return $load->$key else
- return null;
}
see also: http://docs.php.net/property_exists
You can make static variable and then you can access this variable from anywhere
public statis $var = NULL;
and you can access it like this
classname::$var;

Categories