How to assign a trait method to child class object? - php

my question is very easy.I have a trait and child class.
trait mixVar {
public function go(){
return [];
}
}
class example extends foo {
use mixVar;
public $var=$this->go(); //does not work
}
how to make this in php?

You cannot use a method call to assign a default value to a property. You would have to do it in the constructor.
class example extends foo
{
use mixVar;
public $var;
public function __construct()
{
$this->var = $this->go();
}
}

Another solution is to define trait method like:
trait mixVar {
public function go(){
$this->var = []; // your value here
}
}

Related

Can't access construct variable in method ( php laravel )

I have a base class which sets up's other extending controllers like this:
class BaseController extends Controller
{
public $globalCurrencies;
public $globalLanguages;
public function __construct()
{
$this->globalCurrencies = $this->getCurrencies(); // this works
$this->globalLanguages = $this->getLanguages(); // this works
}
}
And I use one of helpers to extend this class like this:
class SessionHelper extends BaseController
{
public $test;
public function __construct()
{
parent::__construct(); // fire parent aka basecontroller construct
$this->test = $this->globalCurrencies; // this works (variables are set)
echo '__construct: '.$this->test; // this even displays it
}
public function getCurrencies()
{
dd('method'.$this->test); // NOT WORKING
}
public function getCurrentCurrency()
{
return $this->getCurrencies()->where('id', Session::get('currencyId'))->first() ?? null;
}
}
Later on code is used in model:
class Product extends Model
{
protected $table = "products";
public $timestamps = true;
public $sessionHelper;
public function __construct()
{
$this->sessionHelper = new SessionHelper;
}
public function getPrice($conversion_rate = null)
{
return number_format($this->price_retail / $this->sessionHelper->getCurrentCurrency()->conversion_rate, 2);
}
}
Have any body idea why I can access in construct variable but not in method? If i remember correctly construct is fired first so everything after should have access to it.
Declare $test variable as private out side the constructor. Inside the constructor keep it the way you are doing it right now and then make a setter and getter for the test variable.
class testObject
{
private $test;
function __construct($test)
{
$this->test= $this->globalCurrencies;
}
// test getter
function getTest()
{
return $this->test;
}
}
Change your method to be;
public function getCurrencies()
{
dd('method', $this->test);
}
You can not concatenate strings and objects/arrays.
If that doesn't resolve the issue - check the laravel.log

Accessing a property from an extended class

<?php
class BaseController extends Controller
{
protected $foo;
public function __construct()
{
$this->foo = '123';
}
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
Above is the BaseController and I want to declare foo as 123, but can I get the foo variable in the controller which I have extended from this basecontroller, can you help?
public function detail($action)
{
return $this->foo;
}
As per docs:
http://php.net/manual/en/language.oop5.decon.php
Note: Parent constructors are not called implicitly if the child class
defines a constructor. In order to run a parent constructor, a call to
parent::__construct() within the child constructor is required.
As you are doing some work in your parent class constructor, you must invoke it directly in your subclass too (even this would be to only thing you do in child's constructor). I.e.:
class ChildController extends BaseController
{
public function __construct() {
parent::__construct();
}
...
When you extend the controller, I imagine that you're currently doing this:
<?php
class NewController extends BaseController
{
public function __construct()
{
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}
You see how the __construct method is being overwritten. You can easily fix this by adding parent::__construct() to the beginning of the method, so you'll have this:
<?php
class NewController extends BaseController
{
public function __construct()
{
parent::__construct();
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}

PHP object-oriented Inheritance - accessing parent's property

I need to access a property of a parent inside a function of the child class. A static variable can accessed with parent:: but how can I access a non-static parent variable when the child class has a variable with the same name?
class My_parent{
$name = "Praeep";
}
class My_child extends My_parent {
$name ="Nadeesha";
function show_name() {
// need to access $name of the parent just referring the parent variable
}
}
You can either declare the variable in the parent class with a protected modifier or provide a getter. The getter approach would be prefered to ensure encapsulation.
class My_parent{
private $name = "Praeep";
public function getName() {
return $this->name;
}
}
class My_child extends My_parent {
public function show_name() {
echo $this->getName();
}
}
If you also want the property to be mutable consider providing a setter as well.
Add a construct function to your parent class and define your variable inside this function.
class My_parent{
public $name;
public function __construct(){
$this->name= "Praeep";
}
}
If your child class has a construct function too, you need to invoke the parents construct function manually. However a class doesn't have to have a construct function so I commented it out for simplicity.
class My_child extends My_parent {
// public function __construct(){
// parent::__construct();
// }
public function show_name(){
echo $this->name;
}
}
$c=new My_child();
$c->show_name();
EDIT:
well in fact you don't need the construct function in the parent class.
class My_parent{
public $name= "Praeep";
}
class My_child extends My_parent {
public function show_name(){
echo $this->name;
}
}
$c=new My_child();
$c->show_name();

Use of magic __set() function in base class

I have tried in yii frame work adding set function in CController and try to add property in controller's object but it gives error
Can i user base class's __set function to set property of a instance.
Class Base {
public function __set() {
$this->$name = $value;
}
}
Class SubClass1 extends Base{
}
Class SubClass2 extends Base {
}
I create instance of SubClass1 or SubClass2 and i want to add property dynamically. Is it possible.
yes you can unless the property is private
Yes you can place your magic function your your class structure. Magic functions normally calls even if you are not defining in class file.
So, if you not create __set() in your class file, it won't affect much. But, if you want to do some extra things while setting under object then it will be helpful.
Also, __set() function need two parameter, $name and $value. Please refer following url,
__set() magic function
Regards
You can indeed, the following will demonstrate:
<?php
class Foo
{
private $vars = array();
public function __set($name, $value)
{
echo "Setting $name: $value<br />";
$this->vars[$name] = $value;
}
public function __get($name)
{
echo "Getting $name<br />";
return isset($this->vars[$name]) ? $this->vars[$name] : null;
}
}
class Bar extends Foo {}
$bar = new Bar();
$bar->baz = 'baz';
echo $bar->baz;
Which will output:
Setting baz: baz
Getting baz
baz
class Base {
protected $data;
public function __construct() {
$this->someMethodBaseClass();
}
protected function someMethodBaseClass() {
$this->data = 123456;
}
}
class subclass1 extends Base{
public function __construct() {
parent::__construct();
}
public function getData() {
return $this->data;
}
}

How to not allow a sub-class method to be defined in PHP

How can I prevent the something method below to be created in the foo class ?
class fooBase{
public function something(){
}
}
class foo extends fooBase{
public function __construct(){
echo $this->something(); // <- should be the parent class method
}
public function something(){
// this method should not be allowed to be created
}
}
Use the final keyword (like in Java etc):
class fooBase{
final public function something(){
}
}
class foo extends fooBase{
public function __construct(){
echo $this->something(); // <- should be the parent class method
}
public function something(){
// this method should not be allowed to be created
}
}
See PHP Final keyword. Note that foo will still have a method something, but something will only come from fooBase and foo can't override it.
Use the final keyword.
In your parent:
final public function something()
You can use final to prevent base methods being overwritten.
class fooBase{
final public function something(){
}
}

Categories