traits

Traits with PHP and Laravel

南笙酒味 提交于 2019-12-21 07:58:34
问题 I am using Laravel 5.1 and would like to access an array on the Model from the Trait when the Model before the model uses the appends array. I would like to add certain items to the appends array if it exists from my trait. I don't want to edit the model in order to achieve this. Are traits actually usable in this scenario or should I use inheritance? array_push($this->appends, 'saucedByCurrentUser'); Here is how my current setup works. Trait <?php namespace App; trait AwesomeSauceTrait { /**

How to prevent use of trait methods out of “use” scope in PHP

为君一笑 提交于 2019-12-21 05:36:05
问题 I'd like to know if there is any way to prevent the use of trait methods out of any class context in PHP ? Let me explain what I want with a short example, here is my current code : // File : MyFunctions.php trait MyFunctions { function hello_world() { echo 'Hello World !'; } } // File : A.php include 'MyFunctions.php'; class A { use MyFunctions; } // File : testTraits.php include 'A.php'; hello_world(); // Call to undefined function -> OK, expected A::hello_world(); // Hello World ! -> OK,

Traits and serialization/deserialization

好久不见. 提交于 2019-12-21 04:38:14
问题 Say I have two traits that I would like to mixin to a class. The traits each implement an abstract method that the class needs. trait Writable { def serialize(out: java.io.DataOutput) } trait T1 extends Writable trait A extends T1 { val aNum: Int abstract override def serialize(out: java.io.DataOutput) = { super.serialize(out) println("A serialize") out.writeInt(aNum) } def action = println("A action") } trait B extends T1 { val bNum: Int abstract override def serialize(out: java.io

PHP 5.4: why can classes override trait methods with a different signature?

社会主义新天地 提交于 2019-12-21 03:43:12
问题 I'm wondering if there is any good reason why this behaviour is possible in the current PHP 5.4 implementation: trait T { public function test(PDO $pdo) {} } class C { use T; public function test(DOMDocument $dom) {} } I thought that the fact that a class uses a trait, guaranteed that this class had a specific interface available. But here, if we inadvertently override the trait method for another purpose, we don't even receive a Strict Standards notice, as with classic inheritance. Is this

how to get used traits in php-class?

∥☆過路亽.° 提交于 2019-12-21 03:14:12
问题 Is there any function in PHP (5.4) to get used traits as array or similar: class myClass extends movingThings { use bikes, tanks; __construct() { echo 'I\'m using the two traits:' . ????; // bikes, tanks } } 回答1: To easily get the used traits you can call class_uses() $usedTraits = class_uses(MyClass); // or $usedTraits = class_uses($myObject); General recommendations When checking for available functionality, I would generally recommend to use interfaces . To add default functionality to an

Scala immutable objects and traits with val fields

醉酒当歌 提交于 2019-12-20 19:42:29
问题 I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable. I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of

How to initialize a trait variable that is a val

拈花ヽ惹草 提交于 2019-12-20 07:08:37
问题 I have MyObject and MyTrait: class MyObject(private val myname: String = "") extends MyTrait { _name = myname def foo(myname : String) { _name = myname } } trait MyTrait { protected var _name: String = _ def name = _name } This works fine as this val myObject = new MyObject("abc") println(myObject.name) myObject.foo("def") println(myObject.name) prints abc def as expected. Problem now is that I want MyTrait._name to be a val instead of a var. But there is no way I can manage to get this to

iOS Use Vary For Traits Between iPhone Sizes

主宰稳场 提交于 2019-12-20 04:27:18
问题 I have a button that I want to be different widths depending on which kind of iPhone is being used. Obviously a larger width for the 7 Plus, and smaller on down the line. I click the button, click Vary for Traits, choose Height, change constraints for each iPhone, then click Done Varying and build, but it always keeps whatever the last constraint I edited was, no matter which device I run it on. 回答1: Vary For Traits has nothing to do with different iPhone sizes. All iPhones (in portrait) have

Do I have to implement a trait twice when implementing it for both reference and non-reference types?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 04:19:11
问题 I want to implement a trait for both a for reference and non-reference type. Do I have to implement the functions twice, or this is not idiomatic to do so? Here's the demo code: struct Bar {} trait Foo { fn hi(&self); } impl<'a> Foo for &'a Bar { fn hi(&self) { print!("hi") } } impl Foo for Bar { fn hi(&self) { print!("hi") } } fn main() { let bar = Bar {}; (&bar).hi(); &bar.hi(); } 回答1: This is a good example for the Borrow trait. use std::borrow::Borrow; struct Bar; trait Foo { fn hi(&self)

Why does Rust not allow coercion to trait objects inside containers?

£可爱£侵袭症+ 提交于 2019-12-19 16:56:09
问题 I have a Vec<Box<T>> where T implements Foo . Why can I not coerce it to a Vec<Box<Foo>> even though I can coerce anything of type Box<T> into a Box<Foo> ? Why does the below code not compile? use std::vec; trait Foo {} struct Bar {} impl Foo for Bar {} fn main() { let v = vec![Box::new(Bar {})]; let v_1 = v as Vec<Box<Foo>>; } 回答1: Because Box<Bar> is a different size than Box<Foo> . The coercion is allowed on a single value, but here you'd have to resize the whole vector. The book goes into