First of all, I want to write two class.
<?php
class A
{
function load($v)
{
$this->$v = "stack";
}
}
class Base
{
var $lib = null;
function __construct()
{
$this->lib = new A();
}
}
$bs = new Base();
$bs->lib->load("libx");
// Current calling method
$bs->lib->libx;
// But I want this
$bs->libx;
?>
I write a MVC fremawork.
CodeIgniter can do this but I could not.
My english is poor. Because of this, don't talk me complicated please.
You can add a magic __get method to Base to look for the property in lib, and return it if it exists:
class A
{
function load($v)
{
$this->$v = "stack";
}
}
class Base
{
var $lib = null;
function __construct()
{
$this->lib = new A();
}
public function __get($v)
{
if (property_exists($this->lib, $v)) {
return $this->lib->$v;
}
}
}
Usage:
$bs = new Base();
$bs->lib->load("libx");
echo $bs->lib->libx, PHP_EOL;
echo $bs->libx, PHP_EOL;
Output:
stack
stack
Related
I try to create object in PHP class, but i get some interesting errors in IDE, like unexpected ( token etc. Here is my code:
class A {
public $a = 1;
}
class B {
$aa = new A();
}
Where is the problem?
In PHP, you can only assign "fixed" values to properties in the class definition.
class A {
public $a = 3; // will work
public $b = "hello"; // will work
public $c = foo(); // won't work
public $d = new Foo(); // won't work
}
If you want to do so, you can use the __construct() method which will be called every time a new instance is created or any other method that you call.
class B {
public $aa; // define visibility of $aa
function __construct() {
$this->aa = new A();
}
}
You need to make a constructor on class A
class A {
function __construct() {
$this->a = 1;
}
public function returnA() {
return $this->a;
}
}
$aa = new A();
echo $aa->returnA();
Try to create a constructor in class A and see if it works:
class A {
public $a;
function __construct()
{
$this->$a = 1;
}
}
class B {
$aa = new A();
}
Is there any way to make it work? See example :
class car {
function __construct($type){
switch ($type) {
case 'big' : return new big_car();
case 'small' : return new small_car();
}
}
function whatisit () {
echo "this is car ;( \n";
}
}
class big_car extends car {
function __construct(){}
function whatisit () {
echo "this is big car ;) \n";
}
}
class small_car extends car {
function __construct(){}
function whatisit () {
echo "this is small car ;) \n";
}
}
so the goal is to use it this way:
$mycar = new car('big');
$mycar->whatisit(); // i want it to say that it's big
I guess very much that its bad way and it cannot work this way but maybe there is a trick?
PS:: I know I can use special static method for that but...
You need a car factory to create new cars; this is not JavaScript :)
class car_factory
{
function create_car($type = null)
{
switch ($type) {
case 'big':
return new big_car();
case 'small':
return new small_car();
case null:
return new car();
}
throw new InvalidArgumentException($type);
}
}
$factory = new car_factory;
$small_car = $factory->create_car('small');
$std_car = $factory->create_car();
Of course, you should remove the __construct function from your original code.
As mentioned in the comments you could completely generalize this by using dynamic classes, assuming class extensions have the same constructor and class naming is consistent:
class car_factory
{
function create_car($type = null)
{
if (is_null($type)) {
return new car();
}
$class = "{$type}_car";
if (class_exists($class)) {
$obj = new $class();
if ($obj instanceof car) {
return $obj;
}
}
throw new InvalidArgumentException($type);
}
}
Personally I have no preferences either way; if extensibility is a key factor, go for it, otherwise stick with a simple switch.
[...]you could completely generalize this by using dynamic classes, assuming class extensions have the same constructor and class naming is consistent
You can add even more flexibility using Reflection:
class car_factory
{
function create_car($class = null, $constructorArgs = array())
{
if (is_null($class)) {
return new car();
}
try {
$refl = new ReflectionClass($class);
if (!$refl->isSubclassOf('car') {
throw new DomainException("Type: {$class} is not a car type");
}
return $refl->newIntanceArgs($constructorArgs);
} catch(ReflectionException $e) {
throw new DomainException("Invalid car type: {$class}");
}
}
}
Now use:
$factory = new car_factory();
$car1 = $factory->create_car('big_car');
$car2 = $factory->create_car('small_car');
$car3 = $factory->create_car('fancy_car_name_with_constructor_args', array("I'm fancy!"));
PHP calls private method in parent class instead of method define in current class called by call_user_func
class Car {
public function run() {
return call_user_func(array('Toyota','getName')); // should call toyota
}
private static function getName() {
return 'Car';
}
}
class Toyota extends Car {
public static function getName() {
return 'Toyota';
}
}
$car = new Car();
echo $car->run(); //Car instead of Toyota
$toyota = new Toyota();
echo $toyota->run(); //Car instead of Toyota
I have found a solution with a different approach..
<?php
class Car {
public static function run() {
return static::getName();
}
private static function getName() {
return 'Car';
}
}
class Toyota extends Car {
public static function getName() {
return 'Toyota';
}
}
echo Car::run();
echo Toyota::run();
?>
Using Late Static Binding..
You might use something like this:
<?php
class Car {
public function run() {
return static::getName();
}
private static function getName(){
return 'Car';
}
}
class Toyota extends Car {
public static function getName(){
return 'Toyota';
}
}
$car = new Car();
echo $car->run();
echo PHP_EOL;
$toyota = new Toyota();
echo $toyota->run();
?>
Output:
Car
Toyota
PHP 5.4.5
This is a bug that appears to have fluctuated in and out of existence for a long time (see #deceze's tests in comments on the question). It is possible to "fix" this issue - that is, give consistent behaviour across PHP versions - using reflection:
Works in PHP 5.3.2 and later due to a dependency on ReflectionMethod::setAccessible() to invoke private/protected methods. I will add further explanation for this code, what it can and can't do and how it works very shortly.
Unfortunately it's not possible to test this directly on 3v4l.org because the code is too large, however this is the first ever real use case for minifying PHP code - it does work on 3v4l if you do this, so feel free to play around and see if you can break it. The only issue I'm aware of is that it doesn't currently understand parent. It is also restricted by the lack of $this support in closures before 5.4, there's not really anything that can be done about this though.
<?php
function call_user_func_fixed()
{
$args = func_get_args();
$callable = array_shift($args);
return call_user_func_array_fixed($callable, $args);
}
function call_user_func_array_fixed($callable, $args)
{
$isStaticMethod = false;
$expr = '/^([a-z_\x7f-\xff][\w\x7f-\xff]*)::([a-z_\x7f-\xff][\w\x7f-\xff]*)$/i';
// Extract the callable normalized to an array if it looks like a method call
if (is_string($callable) && preg_match($expr, $callable, $matches)) {
$func = array($matches[1], $matches[2]);
} else if (is_array($callable)
&& count($callable) === 2
&& isset($callable[0], $callable[1])
&& (is_string($callable[0]) || is_object($callable[0]))
&& is_string($callable[1])) {
$func = $callable;
}
// If we're not interested in it use the regular mechanism
if (!isset($func)) {
return call_user_func_array($func, $args);
}
$backtrace = debug_backtrace(); // passing args here is fraught with complications for backwards compat :-(
if ($backtrace[1]['function'] === 'call_user_func_fixed') {
$called = 'call_user_func_fixed';
$contextKey = 2;
} else {
$called = 'call_user_func_array_fixed';
$contextKey = 1;
}
try {
// Get a reference to the target static method if possible
switch (true) {
case $func[0] === 'self':
case $func[0] === 'static':
if (!isset($backtrace[$contextKey]['object'])) {
throw new Exception('Use of self:: in an invalid context');
}
$contextClass = new ReflectionClass($backtrace[$contextKey][$func[0] === 'self' ? 'class' : 'object']);
$contextClassName = $contextClass->getName();
$method = $contextClass->getMethod($func[1]);
$ownerClassName = $method->getDeclaringClass()->getName();
if (!$method->isStatic()) {
throw new Exception('Attempting to call instance method in a static context');
}
$invokeContext = null;
if ($method->isPrivate()) {
if ($ownerClassName !== $contextClassName
|| !method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call private method in an invalid context');
}
$method->setAccessible(true);
} else if ($method->isProtected()) {
if (!method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call protected method in an invalid context');
}
while ($contextClass->getName() !== $ownerClassName) {
$contextClass = $contextClass->getParentClass();
}
if ($contextClass->getName() !== $ownerClassName) {
throw new Exception('Attempting to call protected method in an invalid context');
}
$method->setAccessible(true);
}
break;
case is_object($func[0]):
$contextClass = new ReflectionClass($func[0]);
$contextClassName = $contextClass->getName();
$method = $contextClass->getMethod($func[1]);
$ownerClassName = $method->getDeclaringClass()->getName();
if ($method->isStatic()) {
$invokeContext = null;
if ($method->isPrivate()) {
if ($ownerClassName !== $contextClassName || !method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call private method in an invalid context');
}
$method->setAccessible(true);
} else if ($method->isProtected()) {
if (!method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call protected method in an invalid context');
}
while ($contextClass->getName() !== $ownerClassName) {
$contextClass = $contextClass->getParentClass();
}
if ($contextClass->getName() !== $ownerClassName) {
throw new Exception('Attempting to call protected method in an invalid context');
}
$method->setAccessible(true);
}
} else {
$invokeContext = $func[0];
}
break;
default:
$contextClass = new ReflectionClass($backtrace[$contextKey]['object']);
$method = new ReflectionMethod($func[0], $func[1]);
$ownerClassName = $method->getDeclaringClass()->getName();
if (!$method->isStatic()) {
throw new Exception('Attempting to call instance method in a static context');
}
$invokeContext = null;
if ($method->isPrivate()) {
if (empty($backtrace[$contextKey]['object'])
|| $func[0] !== $contextClass->getName()
|| !method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call private method in an invalid context');
}
$method->setAccessible(true);
} else if ($method->isProtected()) {
$contextClass = new ReflectionClass($backtrace[$contextKey]['object']);
if (empty($backtrace[$contextKey]['object']) || !method_exists($method, 'setAccessible')) {
throw new Exception('Attempting to call protected method outside a class context');
}
while ($contextClass->getName() !== $ownerClassName) {
$contextClass = $contextClass->getParentClass();
}
if ($contextClass->getName() !== $ownerClassName) {
throw new Exception('Attempting to call protected method in an invalid context');
}
$method->setAccessible(true);
}
break;
}
// Invoke the method with the passed arguments and return the result
return $method->invokeArgs($invokeContext, $args);
} catch (Exception $e) {
trigger_error($called . '() expects parameter 1 to be a valid callback: ' . $e->getMessage(), E_USER_ERROR);
return null;
}
}
Use "protected" modifier if you want to get access from parent and descendants only. IMO, it's obvious. For example:
<?php
class Car {
public function run() {
return call_user_func(array('static','getName'));
}
protected static function getName() {
return 'Car';
}
}
class Toyota extends Car {
protected static function getName() {
return 'Toyota';
}
}
$car = new Car();
echo $car->run(); // "Car"
$toyota = new Toyota();
echo $toyota->run(); // "Toyota"
You can use get_called_class() instead of 'static'.
The problem is, I think, with the different access levels of the two getname functions. If you make the base class version of getname() public (the same as the derived class version), then in php 5.3.15 (on my Mac), you get Toyota. I think that, because of the different access levels, you end up with two different versions of the getname() function in the Toyota class, rather than the derived class version overriding the base class version. In other words, you have overloading rather than overriding. Therefore, when the run() function looks for a getname() function in the Toyota class to execute, it finds two and takes the first one, which would be the first to be declared (from the base class).
Granted this is just supposition on my part, but it sounds plausible.
use the get_called_called function todo this
public function run() {
$self = get_called_class();
return $self::getName();
}
I believe you're functions are overriding each other and by default going to the first one. Unless you change the parameters of one function, or rename the function it will always default to the parent class function.
hello im still on learning mvc by makeing one, and today i realize that i have a miss on how things work.
class Framework
{
function __construct()
{
require 'libraries/language/l.php';
/*
$l['hello'] = 'hello';
$l['helloworld'] = 'helloworld';
etc
*/
}
}
class Controller extends Framework
{
function index()
{
#missing ?
echo $l;
}
}
ok the first question is how can i echo $l from my controller files ? is there a way to do that ?
edit* same for this.
function library( $lib ){
if (file_exists('libraries/lib.'. $lib .'.php')) {
require 'libraries/lib.'. $lib .'.php';
if (class_exists($lib)) {
$class = ucfirst($lib);
$$lib = new $class;
return TRUE;
}
if (!class_exists($lib)) {
return FALSE;
}
}
}
thanks for looking in.
Adam ramadhan
Pass the data through object protected properties:
class Framework
{
protected $l = array();
function __construct()
{
require 'libraries/language/l.php';
$this->l['hello'] = 'hello';
$this->l['helloworld'] = 'helloworld';
}
}
class Controller extends Framework
{
function index()
{
echo $this->l['hello'];
}
}
Well, that means for each Controller instance, you are going to keep a big array inside it.
Actually, you can make a singleton class that provides translation for text:
class Language
{
private static $instance;
public $l = array();
private function __construct() {
require 'libraries/language/l.php';
$this->l = $l;
}
public static function getInstance() {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
}
And you can have a shorthand function for it:
function l($text) {
return Language::getInstance()->l[$text];
}
And then use it:
echo l('hello') . "\n";
define('anActionType', 1);
$actionTypes = array(anActionType => 'anActionType');
class core {
public $callbacks = array();
public $plugins = array();
public function __construct() {
$this->plugins[] = new admin();
$this->plugins[] = new client();
}
}
abstract class plugin {
public function registerCallback($callbackMethod, $onAction) {
if (!isset($this->callbacks[$onAction]))
$this->callbacks[$onAction] = array();
global $actionTypes;
echo "Calling $callbackMethod in $callbacksClass because we got {$actionTypes[$onAction]}" . PHP_EOL;
// How do I get $callbacksClass?
$this->callbacks[$onAction][] = $callbackMethod;
}
}
class admin extends plugin {
public function __construct() {
$this->registerCallback('onTiny', anActionType);
}
public function onTiny() { echo 'tinyAdmin'; }
}
class client extends plugin {
public function __construct() {
$this->registerCallback('onTiny', anActionType);
}
public function onTiny() { echo 'tinyClient'; }
}
$o = new core();
$callbacksClass should be admin or client. Or am I missing the point here completely and should go about this another way? It should be noted that I will only accept an answer that does not require me to send the classname as an argument to the registerCallback method.
If anyone came here looking for how to get the name of a calling class from another class like I did, check this out https://gist.github.com/1122679
EDIT: pasted code
function get_calling_class() {
//get the trace
$trace = debug_backtrace();
// Get the class that is asking for who awoke it
$class = $trace[1]['class'];
// +1 to i cos we have to account for calling this function
for ( $i=1; $i<count( $trace ); $i++ ) {
if ( isset( $trace[$i] ) ) // is it set?
if ( $class != $trace[$i]['class'] ) // is it a different class
return $trace[$i]['class'];
}
}
EG
class A {
function t() {
echo get_calling_class();
}
}
class B {
function x() {
$a = new A;
$a->t();
}
}
$b = new B;
$b->x(); // prints B
Use get_class():
$this->callbacks[$onAction][] = $callbackMethod;
$className = get_class($this);
// Call callback method
$className->$callbackMethod();
You should really do something like:
$this->registerCallback(array($this, 'onTiny'), anActionType);
That is how PHP works with handles to object methods.
From PHP 8+ you can use static::class rather than get_class($this).
This one is also auto-fixed with PHP Code Sniffer and rule SlevomatCodingStandard.Classes.ModernClassNameReference