Search Results

Search found 3120 results on 125 pages for 'php5 oop'.

Page 51/125 | < Previous Page | 47 48 49 50 51 52 53 54 55 56 57 58  | Next Page >

  • Is it possible to cancel function override in parent class and use function from top level parent

    - by Anatoliy Gusarov
    class TopParent { protected function foo() { $this->bar(); } private function bar() { echo 'Bar'; } } class MidParent extends TopParent { protected function foo() { $this->midMethod(); parent::foo(); } public function midMethod() { echo 'Mid'; } public function generalMethod() { echo 'General'; } } Now the question is if I have a class, that extends MidParent because I need to call class Target extends MidParent { //How to override this method to return TopParent::foo(); ? protected function foo() { } } So I need to do this: $mid = new MidParent(); $mid->foo(); // MidBar $taget = new Target(); $target->generalMethod(); // General $target->foo(); // Bar UPDATE Top parent is ActiveRecord class, mid is my model object. I want to use model in yii ConsoleApplication. I use 'user' module in this model, and console app doesn't support this module. So I need to override method afterFind, where user module is called. So the Target class is the class that overrides some methods from model which uses some modules that console application doesn't support.

    Read the article

  • Best way to design a class in python

    - by Fraz
    So, this is more like a philosophical question for someone who is trying to understand classes. Most of time, how i use class is actually a very bad way to use it. I think of a lot of functions and after a time just indent the code and makes it a class and replacing few stuff with self.variable if a variable is repeated a lot. (I know its bad practise) But anyways... What i am asking is: class FooBar: def __init__(self,foo,bar): self._foo = foo self._bar = bar self.ans = self.__execute() def __execute(self): return something(self._foo, self._bar) Now there are many ways to do this: class FooBar: def __init__(self,foo): self._foo = foo def execute(self,bar): return something(self._foo, bar) Can you suggest which one is bad and which one is worse? or any other way to do this. This is just a toy example (offcourse). I mean, there is no need to have a class here if there is one function.. but lets say in __execute something() calls a whole set of other methods.. ?? Thanks

    Read the article

  • Python - Is it possible to get the name of the chained function?

    - by user1326876
    I'm working on a class that basically allows for method chaining, for setting some attrbutes for different dictionaries stored. The syntax is as follows: d = Test() d.connect().setAttrbutes(Message=Blah, Circle=True, Key=True) But there can also be other instances, so, for example: d = Test() d.initialise().setAttrbutes(Message=Blah) Now I believe that I can overwrite the "setattrbutes" function; I just don't want to create a function for each of the dictionary. Instead I want to capture the name of the previous chained function. So in the example above I would then be given "connect" and "initialise" so I know which dictionary to store these inside. I hope this makes sense. Any ideas would be greatly appreciated :)

    Read the article

  • [[alloc] init] as a factory method

    - by iter
    I want to initialize an instance of one of the subclasses of a superclass depending on the arguments to init: [[Vehicle alloc] initWithItinerary: shortWay]; // returns a bicycle [[Vehicle alloc] initWithItinerary: longWay]; // returns a car I can't find examples of code like this. I wonder if this is not idiomatic in Objective C, or I simply am not looking in the right places.

    Read the article

  • Class member variables, methods and their state

    - by codeMonkey
    How should class member variables be used in combination with class methods? Let's say I have a class 'C' with a member variable 'someData'. I call C.getData(), which does not return a value but instead puts data in C.someData. The class that instantiated 'C' first calls C.getData and then uses the data by accessing the member variable C.someData. I call C.getData() in the class that instantiated 'C' which is a function that returns data. I myself prefer the second way. But it also depends on the situation and it's a small difference. Is it 'bad' to have class methods that depend on the classes internal state? What are the best conventions?

    Read the article

  • How to structure a bigger/complicated project?

    - by Per
    Hello, I'm creating this (big?) project in high school where I'm programming the game Shithead (card game) (so that two people can play against eachother). I'll use mostly PHP, MySQL, JavaScript and Ajax. But I have never made a project like this before. I have only made like CMS-systems and so on. Do you guys have any tips on how I should structure this project? I'm also not so used to Object-oriented programming, but I guess I should consider this. Should I, for example, make a class for the cards? I'm very thankful for any tips or good links!

    Read the article

  • What pattern is this? php

    - by user151841
    I have several classes that are basically interfaces to database rows. Since the class assumes that a row already exists ( __construct expects a field value ), there is a public static function that allows creation of the row and returns an instance of the class. Here's an example ( without the actual database inserts ): class selfStarter { public $type; public function __construct( $type ) { $this->type = $type; } public static function create( $type ) { if ( ! empty($type) ) { $starter = & new selfStarter($type); return $starter; } } } $obj1 = selfStarter::create( "apple" ); $obj2 = & new selfStarter( "banana" ); What is this pattern called?

    Read the article

  • AS3 Passing data between objects/classes

    - by 1337holiday
    So i a building a categorized menu of different foods. I have a class for "categories" (buttons) which essentially will return a string "salads", "drinks", etc. I now have another class "menuItems" for items within categories and this handles sizes such as "small", "med", "large", etc. My problem now is that when i return "salads", i want to invoke an array which contains all the elements of salads, send it to menuItems which will populate the menu. So far i have both the category objects and the menu object setup. I just cant seem to be able to send the data that the category object is returning and pass it to the menu object. Both of which are added to the stage as shown below: If there was a way that i could say add all these classes to one class so that they can talk to each other that would be great but i dont know how to do this. Been stuck for hours, please any help is greatly appreciated.

    Read the article

  • Refactoring huge if ( ... instanceof ...)

    - by Chris
    I'm currently trying to refactor a part of a project that looks like this: Many classes B extends A; C extends A; D extends C; E extends B; F extends A; ... And somewhere in the code: if (x instanceof B){ B n = (B) x; ... }else if (x instanceof C){ C n = (C) x; ... }else if (x instanceof D){ D n = (D) x; ... }else if (x instanceof E){ E n = (E) x; ... }else if (x instanceof G){ G n = (G) x; ... }... Above if-construct currently sits in a function with a CC of 19. Now my question is: Can I split this if-construct up in mutliple functions and let Java's OO do the magic? Or are there any catches I have to look out for? My idea: private void oopMagic(C obj){ ... Do the stuff from the if(x instanceof C) here} private void oopMagic(D obj){ ... Do the stuff from the if(x instanceof D) here} private void oopMagic(E obj){ ... Do the stuff from the if(x instanceof E) here} .... and instead of the huge if: oopMagic(x);

    Read the article

  • Accessing a function of an instance which is in an arraylist

    - by fadeir
    I'm tring to access a function of an instance which is in an arraylist. Is there any way to do that without using the class name of the instance? import java.util.ArrayList; import java.util.List; class apple{ int price; public void myFunction(int iPrice) { price=iPrice; } } class orange{ int price; public void myFunction(int iPrice) { price=iPrice; } } public class main { public static void main(String[] args) { List list= new ArrayList(); //create 3 apple object to list list.add( new apple() ); list.add( new apple() ); list.add( new orange() ); list.get(0).myFunction(1); /* Error: The method myFunction(int) is undefined for the type Object*/ } } I know that;((apple) list.get(0)).myFunction(1); is a way but I'dont want to use any class name while calling the function.

    Read the article

  • PHP inheriting/extending a particular instance of an Object

    - by delta9
    Is there any way to force PHP to extend an existing/particular (and for that matter, already instantiated) instance of an object? This imaginary code should explain what I am wondering: class Base { public $var; function __construct($var){ $this->var = $name; } } class Extender extends Base { function __construct($parent) { parent = $parent; } } $base = new Base('yay!'); $extender = new Extender($base); echo 'Extended base var value: '.$extender->var.'<br/>'; Output (would be): Extended base var value: yay! To be clear, I am wanting to instantiate an object that extends a PARTICULAR INSTANCE of another object, one that has already been instantiated. I am aware that I can pass a reference to an object to another object (via it's constructor function) and then add it as a property of the receiving object, I'm wondering if there is a real way to do this?

    Read the article

  • How to access constant defined in child class?

    - by kavoir.com
    I saw this example from php.net: <?php class MyClass { const MY_CONST = "yonder"; public function __construct() { $c = get_class( $this ); echo $c::MY_CONST; } } class ChildClass extends MyClass { const MY_CONST = "bar"; } $x = new ChildClass(); // prints 'bar' $y = new MyClass(); // prints 'yonder' ?> But $c::MY_CONST is only recognized in version 5.3.0 or later. The class I'm writing may be distributed a lot. Basically, I have defined a constant in ChildClass and one of the functions in MyClass (father class) needs to use the constant. Any idea?

    Read the article

  • Updating the Jpanel of a class

    - by ivor
    Hi, After some advice on using jpanel - I'm new to java and playing around with the GUI elements. Bascially what I'm curious about is if I can set up a Jpanel in one class, then somehow add labels etc to the that container, but from another class. Is this possible ? or do i have to set the entire GUI up in one class, but then I guess I would have the same issue, if I wanted to update those fields I had set up in the main class from another class? Apologies I don't really have any code that's usefull to demostrate here - I'm just trying to get the idea going, working out if its possible before I go ahead. And I'm not even sure if this is possible. Any advice would be greatly appreciated. Thanks

    Read the article

  • I'm confused about Polymorphism

    - by Vtanathip
    I'm know polymorphism rule that we can send it via parameter like this code interface Animal { void whoAmI(); } class A implements Animal{ @Override public void whoAmI() { // TODO Auto-generated method stub System.out.println("A"); } } class B implements Animal{ @Override public void whoAmI() { // TODO Auto-generated method stub System.out.println("B"); } } class RuntimePolymorphismDemo { public void WhoRU(List t){ System.out.println(t.getClass()); } public static void main(String[] args) { A a = new A(); B b = new B(); RuntimePolymorphismDemo rp = new RuntimePolymorphismDemo(); rp.WhoRU(a); rp.WhoRU(b); } } but List<Example> examples = new ArrayList<Example>(); This code,I'm don't understand why we must use List. why we can't use like this? ArrayList<Example> examples = new ArrayList<Example>(); Because when we use List we can't use method that only have in ArrayList class like trimToSize() and How I know when to use or not use?

    Read the article

  • How to implement copy operator for such C++ structure?

    - by Kabumbus
    So having struct ResultStructure { ResultStructure(const ResultStructure& other) { // copy code in here ? using memcpy ? how??? } ResultStructure& operator=(const ResultStructure& other) { if (this != &other) { // copy code in here ? } return *this } int length; char* ptr; }; How to implement copy? (sorry - I am C++ nube)

    Read the article

  • Can you explain this generics behavior and if I have a workaround?

    - by insta
    Sample program below: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericsTest { class Program { static void Main(string[] args) { IRetrievable<int, User> repo = new FakeRepository(); Console.WriteLine(repo.Retrieve(35)); } } class User { public int Id { get; set; } public string Name { get; set; } } class FakeRepository : BaseRepository<User>, ICreatable<User>, IDeletable<User>, IRetrievable<int, User> { // why do I have to implement this here, instead of letting the // TKey generics implementation in the baseclass handle it? //public User Retrieve(int input) //{ // throw new NotImplementedException(); //} } class BaseRepository<TPoco> where TPoco : class,new() { public virtual TPoco Create() { return new TPoco(); } public virtual bool Delete(TPoco item) { return true; } public virtual TPoco Retrieve<TKey>(TKey input) { return null; } } interface ICreatable<TPoco> { TPoco Create(); } interface IDeletable<TPoco> { bool Delete(TPoco item); } interface IRetrievable<TKey, TPoco> { TPoco Retrieve(TKey input); } } This sample program represents the interfaces my actual program uses, and demonstrates the problem I'm having (commented out in FakeRepository). I would like for this method call to be generically handled by the base class (which in my real example is able to handle 95% of the cases given to it), allowing for overrides in the child classes by specifying the type of TKey explicitly. It doesn't seem to matter what parameter constraints I use for the IRetrievable, I can never get the method call to fall through to the base class. Also, if anyone can see an alternate way to implement this kind of behavior and get the result I'm ultimately looking for, I would be very interested to see it. Thoughts?

    Read the article

  • Constructors + Dependency Injection

    - by Sunny
    If I am writing up a class with more than 1 constructor parameter like: class A{ public A(Dependency1 d1, Dependency2 d2, ...){} } I usually create a "argument holder"-type of class like: class AArgs{ public Dependency1 d1 { get; private set; } public Dependency2 d2 { get; private set; } ... } and then: class A{ public A(AArgs args){} } Typically, using a DI-container I can configure the constructor for dependencies & resolve them & so there is minimum impact when the constructors need to change. Is this considered an anti-pattern and/or any arguments against doing this?

    Read the article

  • Best way to make an attribute always an array?

    - by Shadowfirebird
    I'm using my MOO project to teach myself Test Driven Design, and it's taking me interesting places. For example, I wrote a test that said an attribute on a particular object should always return an array, so -- t = Thing.new("test") p t.names #-> ["test"] t.names = nil p t.names #-> [] The code I have for this is okay, but it doesn't seem terribly ruby to me: class Thing def initialize(names) self.names = names end def names=(n) n = [] if n.nil? n = [n] unless n.instance_of?(Array) @names = n end attr_reader :names end Is there a more elegant, Ruby-ish way of doing this? (NB: if anyone wants to tell me why this is a dumb test to write, that would be interesting too...)

    Read the article

  • JavaScript prototype(ing) question

    - by OneNerd
    Trying to grasp Prototyping in Javascript. Trying to create my own namespace to extend the String object in JavaScript. Here is what I have so far (a snippet): var ns { alert: function() { alert (this); } } String.prototype.$ns = ns; As you can see, I am trying to place what will be a series of functions into the ns namespace. So I can execute a command like this: "hello world".$ns.alert(); But the problem is, the this doesn't seem to reference the text being sent (in this case, "hello world"). What I get is an alert box with the following: [object Object] Not having a full grasp of the object-oriented nature of JavaScript, I am at a loss, but I am guessing I am missing something simple. Does anyone know how to achieve this? Thanks -

    Read the article

  • Do you use Python mostly for its functional or object-oriented features?

    - by Eric
    I see what seems like a majority of Python developers on StackOverflow endorsing the use of concise functional tools like lambdas, maps, filters, etc., while others say their code is clearer and more maintainable by not using them. What is your preference? Also, if you are a die-hard functional programmer or hardcore into OO, what other specific programming practices do you use that you think are best for your style? Thanks in advance for your opinions!

    Read the article

  • How important is it that models be consistent across project components?

    - by RonLugge
    I have a project with two components, a server-side component and a client-side component. For various reasons, the client-side device doesn't carry a fully copy of the database around. How important is it that my models have a 1:1 correlation between the two sides? And, to extend the question to my bigger concern, are there any time-bombs I'm going to run into down the line if they don't? I'm not talking about having different information on each side, but rather the way the information is encapsulated will vary. (Obviously, storage mechanisms will also vary) The server side will store each user, each review, each 'item' with seperate tables, and create links between them to gather data as necessary. The client side shouldn't have a complete user database, however, so rather than link against the user for gathering things like 'name', I'd store that on the review. In other words... --- Server Side --- Item: +id //Store stuff about the item User: +id +Name -Password Review: +id +itemId +rating +text +userId --- Device Side --- Item: +id +AverageRating Review: +id +rating +text +userId +name User: +id +Name //Stuff The basic idea is that certain 'critical' information gets moved one level 'up'. A user gets the list of 'items' relevant to their query, with certain review-orientation moved up (i. e. average rating). If they want more info, they query the detail view for the item, and the actual reviews get queried and added to the dataset (and displayed). If they query the actual review, the review gets queried and they pick up some additional user info along the way (maybe; I'm not sure if the user would have any use for any of the additional user information). My basic concern is that I don't wan't to glut the user's bandwidth or local storage with a huge variety of information that they just don't need, even if proper database normalizations suggests that information REALLY should be stored at a 'lower' level. I've phrased this as a fairly low-level conceptual issue because that's the level I'm trying to think / worry over, but if it matters I'm creating a PHP / MySQL server that provides data for a iOS / CoreData client.

    Read the article

  • Using Zend_Db and multiple tables

    - by Yacoby
    I have a normalized database that stores locations of files on the internet. A file may have multiple locations spread across different sites. I am storing the urls in two parts (Site.UrlStart, FileLocation.UrlEnd). The UrlEnd is the part unique to that file (for the site). Simplified Db structure: I am using Zend_Db as my ORM (If it is that), with classes for the tables inheriting from Zend_Db_Table_Abstract. The issue is that retrieving the location data (e.g. the url) requires the use of multiple tables and as far as I can make out and I would either have to use both table classes (thereby exposing my table structure) or scatter sql all over my application, neither of which sound appealing. The only solution I can see is to create a façade that acts like Zend_Db_Table_Abstract (Maybe inherits from it?) and hides the fact that the data is actually on two tables. My questions are as follows: Am I going in the right direction in creating a façade class (Are there other alternatives)? Should the facade class inherit from Zend_Db_Table_Abstract?

    Read the article

< Previous Page | 47 48 49 50 51 52 53 54 55 56 57 58  | Next Page >